XMLUtilJS - Global

  • Versão de lançamento: Zurich
  • Atualizado 31 de jul. de 2025
  • 3 min. de leitura
  • . XMLUtilJS A inclusão de script fornece métodos de utilitário XML para JavaScript ser usado com Descoberta scripts.

    Esta inclusão de script é fornecida com Descoberta plug-in. Use esta inclusão de script em qualquer lado do servidor Descoberta Script no qual você precisa de utilitários XML.

    Acesse esses métodos usando a variável estática XMLUtilJS .

    XMLUtilJS - escapeForXMLText (texto de cadeia de caracteres)

    Fornece texto de escape para uma determinada cadeia de caracteres.

    Tabela 1. Parâmetros
    Nome Tipo Descrição
    texto Cadeia de caracteres O texto a ser formatado.
    Tabela 2. Retornos
    Tipo Descrição
    Cadeia de caracteres O texto formatado.

    O exemplo a seguir mostra como listar dados XML de 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.

    Tabela 3. Parâmetros
    Nome Tipo Descrição
    str Cadeia de caracteres A cadeia de caracteres a ser convertida.
    Tabela 4. Retornos
    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(texto de cadeia de caracteres)

    Fornece XML sem escape para uma determinada cadeia de caracteres.

    Tabela 5. Parâmetros
    Nome Tipo Descrição
    texto Cadeia de caracteres O texto XML a ser limpo.
    Tabela 6. Retornos
    Tipo Descrição
    Cadeia de caracteres A cadeia de caracteres XML limpa.

    O exemplo a seguir mostra como converter uma cadeia de caracteres XML de 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.

    Tabela 7. Parâmetros
    Nome Tipo Descrição
    XMLvalue Cadeia de caracteres O XML a ser convertido
    Tabela 8. Retornos
    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>