XMLUtilJS - Global
Fornece utilitários XML para JavaScript a serem usados com scripts Descoberta.
A API XMLUtilJS é fornecida com o plug-in Descoberta. Use esta inclusão de script em qualquer script do lado do servidor Descoberta em que você precise de utilitários XML.
Acesse esses métodos usando a variável estática XMLUtilJS.
XMLUtilJS - escapeForXMLText(cadeia de caracteres texto)
Fornece texto de escape para uma determinada cadeia de caracteres.
| Nome | Tipo | Descrição |
|---|---|---|
| texto | Cadeia de caracteres | O texto a ser formatado. |
| Tipo | Descrição |
|---|---|
| Cadeia de caracteres | O texto formatado. |
O exemplo a seguir mostra como listar dados XML com escape em um documento. Consulte também stringToValue().
// Create an XML document to work with
var doc = new XMLDocument2();
// Add an element called catalog
var catalog = doc.createElement('catalog');
// Set current element to the one previously made to add more elements
doc.setCurrentElement(catalog);
// Add an element called catalog
var bookAttribute = doc.createElement('book');
// Add an attribute to the element created
bookAttribute.setAttribute('id', 'bk101');
// Create multiple elements with a defined value
doc.createElementWithTextValue('author', 'Gambardella, Matthew');
doc.createElementWithTextValue('title', 'XML Developer');
doc.createElementWithTextValue('genre', 'Computer');
doc.createElementWithTextValue('price', '44.95');
// Pass the created XML document as a string to the XMLUtilJS API function
var escapedXML = XMLUtilJS.escapeForXMLText(doc.toString());
// Print the escapedXML. If an invalid XML is created, the original value will be returned
gs.print(escapedXML);
Saída:
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101"/><author>Gambardella, Matthew</author><title>XML Developer</title><genre>Computer</genre><price>44.95</price></catalog>
XMLUtilJS - stringToValue(cadeia de caracteres str)
Converte uma cadeia de caracteres em um valor XML.
| Nome | Tipo | Descrição |
|---|---|---|
| str | Cadeia de caracteres | A cadeia de caracteres a ser convertida. |
| Tipo | Descrição |
|---|---|
| Cadeia de caracteres | A cadeia de caracteres especificada convertida em XML. |
O exemplo a seguir mostra como transformar uma cadeia de caracteres de XML com escape em saída XML marcada. Consulte também escapeForXMLText().
//Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()
var escapedXML = '<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer\'s Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>';
//Pass in escaped XML text and store output in a variable
var output = XMLUtilJS.stringToValue(escapedXML);
// If the input value is a string value of NULL, the output will be null, otherwise an unescaped XML string value.
gs.print(output);
Saída:
<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>
XMLUtilJS - unescapeForXMLText(cadeia de caracteres texto)
Fornece XML sem escape para uma determinada cadeia de caracteres.
| Nome | Tipo | Descrição |
|---|---|---|
| texto | Cadeia de caracteres | O texto XML a ser limpo. |
| Tipo | Descrição |
|---|---|
| Cadeia de caracteres | A cadeia de caracteres XML limpa. |
O exemplo a seguir mostra como converter uma cadeia de caracteres XML com escape em saída XML marcada. Consulte também escapeForXMLText().
//Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()
var escapedXML = '<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer\'s Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>';
//Pass in escaped XML text and store output in a variable
var output = XMLUtilJS.unescapeForXMLText(escapedXML);
//Print the escapedXML. If the XML is invalid, the original value will be returned
gs.print(output);
Saída:
<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>
XMLUtilJS - valueToString(cadeia de caracteres XMLvalue)
Converte um valor XML em uma cadeia de caracteres.
| Nome | Tipo | Descrição |
|---|---|---|
| XMLvalue | Cadeia de caracteres | O XML a ser convertido |
| Tipo | Descrição |
|---|---|
| Cadeia de caracteres | O valor XML convertido em uma cadeia de caracteres. |
O exemplo a seguir mostra como converter um documento XML em uma cadeia de caracteres.
//Create an XML document to work with
var doc = new XMLDocument2();
//Add an element called catalog
var catalog = doc.createElement("catalog");
//Set our current element to the one previously made to add further elements
doc.setCurrentElement(catalog);
//Add an element called catalog
var bookAttribute = doc.createElement("book");
//Add an attribute to the element created
bookAttribute.setAttribute("id" , "bk101");
//create multiple elements with a defined value
doc.createElementWithTextValue("author" , "Gambardella, Matthew");
doc.createElementWithTextValue("title" , "XML Developer");
doc.createElementWithTextValue("genre" , "Computer");
doc.createElementWithTextValue("price" , "44.95");
//Pass the created XML document as a string to the XMLUtilJS API function.
var escapedXML = XMLUtilJS.valueToString(doc.toString());
// Print the results
// Returns NULL if XML is invalid
gs.print(escapedXML);
Saída:
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101"/><author>Gambardella, Matthew</author><title>XML Developer</title><genre>Computer</genre><price>44.95</price></catalog>