XMLUtilJS : Global

  • Freigeben Version: Yokohama
  • Aktualisiert 30. Januar 2025
  • 3 Minuten Lesedauer
  • Die XMLUtilJS -Skripteinbindung stellt XML-Dienstprogrammmethoden für JavaScript zur Verwendung mit Discovery -Skripts bereit.

    Diese Skripteinbindung wird mit dem Plugin Discovery bereitgestellt. Verwenden Sie diese Skripteinbindung in einem beliebigen serverseitigen Skript Discovery, in dem Sie XML-Dienstprogramme benötigen.

    Greifen Sie auf diese Methoden mit der statischen Variablen XMLUtilJS zu.

    XMLUtilJS – escapeForXMLText(String text)

    Stellt Escape-Text für eine bestimmte Zeichenfolge bereit.

    Tabelle : 1. Parameter
    Name Typ Beschreibung
    text Zeichenfolge Zu formatierender Text.
    Tabelle : 2. Ergebnisse
    Typ Beschreibung
    Zeichenfolge Formatierter Text.

    Das folgende Beispiel zeigt, wie XML-Daten mit Escape-Zeichen in einem Dokument aufgelistet werden. Siehe auch „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);

    Ausgabe:

    <?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)

    Konvertiert eine Zeichenfolge in einen XML-Wert.

    Tabelle : 3. Parameter
    Name Typ Beschreibung
    str Zeichenfolge Die zu konvertierende Zeichenfolge.
    Tabelle : 4. Rückgaben
    Typ Beschreibung
    Zeichenfolge Die angegebene Zeichenfolge wurde in XML konvertiert.

    Das folgende Beispiel zeigt, wie eine Zeichenfolge von mit Escape-Zeichen versehener XML-Datei in eine mit Tags markierte XML-Ausgabe umgewandelt wird. Siehe auch 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);

    Ausgabe:

    <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(String text)

    Stellt XML ohne Escape-Zeichen für eine bestimmte Zeichenfolge bereit.

    Tabelle : 5. Parameter
    Name Typ Beschreibung
    text Zeichenfolge Der zu bereinigende XML-Text.
    Tabelle : 6. Rückgaben
    Typ Beschreibung
    Zeichenfolge Die bereinigte XML-Zeichenfolge.

    Das folgende Beispiel zeigt, wie eine XML-Zeichenfolge mit Escape-Zeichen in eine XML-Ausgabe mit Tags konvertiert wird. Siehe auch 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);

    Ausgabe:

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

    Konvertiert einen XML-Wert in eine Zeichenfolge.

    Tabelle : 7. Parameter
    Name Typ Beschreibung
    XMLvalue Zeichenfolge Zu konvertierende XML
    Tabelle : 8. Ergebnisse
    Typ Beschreibung
    Zeichenfolge In eine Zeichenfolge konvertierter XML-Wert.

    Das folgende Beispiel zeigt, wie ein XML-Dokument in eine Zeichenfolge konvertiert wird.

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

    Ausgabe:

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