XMLUtilJS : global
L’include de script XMLUtilJS fournit des méthodes utilitaires XML pour JavaScript à utiliser avec Découverte les scripts.
Cet include de script est fourni avec le module d’extension Découverte . Utilisez cet include de script dans tout script côté Découverte serveur dans lequel vous avez besoin d’utilitaires XML.
Accédez à ces méthodes à l’aide de la variable statique XMLUtilJS.
XMLUtilJS : escapeForXMLText(Texte de chaîne)
Fournit un texte d’échappement pour une chaîne donnée.
| Nom | Type | Description |
|---|---|---|
| Texte | Chaîne | Texte à mettre en forme. |
| Type | Description |
|---|---|
| Chaîne | Texte mis en forme. |
L’exemple suivant montre comment répertorier les données XML échappées dans un document. Voir aussi 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);
Sortie :
<?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(Str chaîne)
Convertit une chaîne en valeur XML.
| Nom | Type | Description |
|---|---|---|
| str | Chaîne | Chaîne à convertir. |
| Type | Description |
|---|---|
| Chaîne | La chaîne spécifiée convertie en XML. |
L’exemple suivant montre comment transformer une chaîne de XML échappée en sortie XML balisée. Voir aussi 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);
Sortie :
<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(Texte de chaîne)
Fournit du code XML sans échappement pour une chaîne donnée.
| Nom | Type | Description |
|---|---|---|
| Texte | Chaîne | Le texte XML à nettoyer. |
| Type | Description |
|---|---|
| Chaîne | Chaîne XML nettoyée. |
L’exemple suivant montre comment convertir une chaîne XML avec échappement en sortie XML balisée. Voir aussi 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);
Sortie :
<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(String XMLvalue)
Convertit une valeur XML en chaîne.
| Nom | Type | Description |
|---|---|---|
| Valeur XML | Chaîne | XML à convertir |
| Type | Description |
|---|---|
| Chaîne | Valeur XML convertie en chaîne. |
L’exemple suivant montre comment convertir un document XML en chaîne.
//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);
Sortie :
<?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>