XMLUtilJS : global

  • Rversion finale: Washingtondc
  • Mis à jour 1 févr. 2024
  • 3 minutes de lecture
  • Fournit des utilitaires XML pour JavaScript à utiliser avec Détection des scripts.

    L’API XMLUtilJS est fournie avec le module d’extensionDétection. Utilisez ce script include dans n’importe quel script côté Détection serveur dans lequel vous avez besoin d’utilitaires XML.

    Accédez à ces méthodes à l’aide de la variable statique XMLUtilJS.

    XMLUtilJS : escapeForXMLText(String texte)

    Fournit un texte d’échappement pour une chaîne donnée.

    Tableau 1. Paramètres
    Nom Type Description
    Texte Chaîne Texte à formater.
    Tableau 2. Renvoie
    Type Description
    Chaîne Le 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(String str)

    Convertit une chaîne en valeur XML.

    Tableau 3. Paramètres
    Nom Type Description
    str Chaîne Chaîne à convertir.
    Tableau 4. Renvoie
    Type Description
    Chaîne Chaîne spécifiée convertie en XML.

    L’exemple suivant montre comment transformer une chaîne de XML échappés 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 un XML sans échappement pour une chaîne donnée.

    Tableau 5. Paramètres
    Nom Type Description
    Texte Chaîne Texte XML à nettoyer.
    Tableau 6. Renvoie
    Type Description
    Chaîne Chaîne XML nettoyée.

    L’exemple suivant montre comment convertir une chaîne 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.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.

    Tableau 7. Paramètres
    Nom Type Description
    XMLvalue Chaîne XML à convertir
    Tableau 8. Renvoie
    Type Description
    Chaîne Valeur XML convertie en chaîne.

    L’exemple suivant montre comment convertir un document XML en chaîne de caractères.

    //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>