---
sourceDocument: Referência de API do Xanadu
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/pt-BR/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - pt-BR

ft:publication_title :

    - Referência de API do Xanadu

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# XMLUtilJS - Global

# XMLUtilJS - Global {#ariaid-title1}

* Versão de lançamento: Xanadu
* 
* Atualizado 1 de ago. de 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 min. de leitura

A inclusão de script XMLUtilJS fornece métodos de utilitário XML para JavaScript a serem usados com scripts Descoberta.

Esta inclusão de script é fornecida com o plug-in Descoberta. Use esta inclusão de script em qualquer script Descoberta do lado do servidor 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) {#ariaid-title2}

Fornece texto de escape para uma determinada cadeia de caracteres.
{#r_XUJ-escapeForXMLText_S__table_upj_wfs_pt__entry__3}

| Nome | Tipo | Descrição |
|-|-|-|
| texto | Cadeia de caracteres | O texto a ser formatado. |
[Tabela 1. Parâmetros]

{#r_XUJ-escapeForXMLText_S__table_upj_wfs_pt} {#r_XUJ-escapeForXMLText_S__table_vpj_wfs_pt__entry__2}

| Tipo | Descrição |
|-|-|
| Cadeia de caracteres | O texto formatado. |
[Tabela 2. Retorna]

{#r_XUJ-escapeForXMLText_S__table_vpj_wfs_pt}  
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) {#ariaid-title3}

Converte uma cadeia de caracteres em um valor XML.
{#r_XUJ-stringToValue_S__table_etz_b3s_pt__entry__3}

| Nome | Tipo | Descrição |
|-|-|-|
| str | Cadeia de caracteres | A cadeia de caracteres a ser convertida. |
[Tabela 3. Parâmetros]

{#r_XUJ-stringToValue_S__table_etz_b3s_pt} {#r_XUJ-stringToValue_S__table_ftz_b3s_pt__entry__2}

| Tipo | Descrição |
|-|-|
| Cadeia de caracteres | A cadeia de caracteres especificada foi convertida em XML. |
[Tabela 4. Retorna]

{#r_XUJ-stringToValue_S__table_ftz_b3s_pt}  
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 text) {#ariaid-title4}

Fornece XML sem escape para uma determinada cadeia de caracteres.
{#r_XUJ-unescapeForXMLText_S__table_in5_pgs_pt__entry__3}

| Nome | Tipo | Descrição |
|-|-|-|
| texto | Cadeia de caracteres | O texto XML a ser limpo. |
[Tabela 5. Parâmetros]

{#r_XUJ-unescapeForXMLText_S__table_in5_pgs_pt} {#r_XUJ-unescapeForXMLText_S__table_jn5_pgs_pt__entry__2}

| Tipo | Descrição |
|-|-|
| Cadeia de caracteres | A cadeia de caracteres XML limpa. |
[Tabela 6. Retorna]

{#r_XUJ-unescapeForXMLText_S__table_jn5_pgs_pt}  
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) {#ariaid-title5}

Converte um valor XML em uma cadeia de caracteres.
{#r_XUJ-valueToString_S__table_ix2_4hs_pt__entry__3}

| Nome | Tipo | Descrição |
|-|-|-|
| XMLvalue | Cadeia de caracteres | O XML a ser convertido |
[Tabela 7. Parâmetros]

{#r_XUJ-valueToString_S__table_ix2_4hs_pt} {#r_XUJ-valueToString_S__table_jx2_4hs_pt__entry__2}

| Tipo | Descrição |
|-|-|
| Cadeia de caracteres | O valor XML convertido em uma cadeia de caracteres. |
[Tabela 8. Retorna]

{#r_XUJ-valueToString_S__table_jx2_4hs_pt}  
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>


