---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# XMLUtilJS - Global

# XMLUtilJS - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes to read

The XMLUtilJS script include provides XML utility methods for JavaScript to be used with Discovery scripts.

This script include is provided with the Discovery plugin. Use this script include in any server-side Discovery script in which you need XML utilities.

Access these methods using the static variable XMLUtilJS.

## XMLUtilJS - escapeForXMLText(String text) {#ariaid-title2}

Provides escape text for a given string.
{#r_XUJ-escapeForXMLText_S__table_upj_wfs_pt__entry__3}

| Name | Type | Description |
|-|-|-|
| text | String | The text to format. |
[Table 1. Parameters]

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

| Type | Description |
|-|-|
| String | The formatted text. |
[Table 2. Returns]

{#r_XUJ-escapeForXMLText_S__table_vpj_wfs_pt}  
The following example shows how to list escaped XML data in a document. See also
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);

Output:

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;catalog&gt;&lt;book id="bk101"/&gt;&lt;author&gt;Gambardella, Matthew&lt;/author&gt;&lt;title&gt;XML Developer&lt;/title&gt;&lt;genre&gt;Computer&lt;/genre&gt;&lt;price&gt;44.95&lt;/price&gt;&lt;/catalog&gt;

## XMLUtilJS - stringToValue(String str) {#ariaid-title3}

Converts a string to an XML value.
{#r_XUJ-stringToValue_S__table_etz_b3s_pt__entry__3}

| Name | Type | Description |
|-|-|-|
| str | String | The string to convert. |
[Table 3. Parameters]

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

| Type | Description |
|-|-|
| String | The specified string converted to XML. |
[Table 4. Returns]

{#r_XUJ-stringToValue_S__table_ftz_b3s_pt}  
The following example shows how to transform a string of escaped XML to tagged XML output.
See also escapeForXMLText().

    //Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()
    var escapedXML = '&lt;catalog&gt;&lt;book id="bk101"&gt;&lt;author&gt;Gambardella, Matthew&lt;/author&gt;&lt;title&gt;XML Developer\'s Guide&lt;/title&gt;&lt;genre&gt;Computer&lt;/genre&gt;&lt;price&gt;44.95&lt;/price&gt;&lt;publish_date&gt;2000-10-01&lt;/publish_date&gt;&lt;description&gt;An in-depth look at creating applications with XML.&lt;/description&gt;&lt;/book&gt;&lt;/catalog&gt;';

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

Output:

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

Provides un-escaped XML for a given string.
{#r_XUJ-unescapeForXMLText_S__table_in5_pgs_pt__entry__3}

| Name | Type | Description |
|-|-|-|
| text | String | The XML text to clean up. |
[Table 5. Parameters]

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

| Type | Description |
|-|-|
| String | The cleaned up XML string. |
[Table 6. Returns]

{#r_XUJ-unescapeForXMLText_S__table_jn5_pgs_pt}  
The following example shows how to convert an escaped XML string to tagged XML output. See
also escapeForXMLText().

    //Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()
    var escapedXML = '&lt;catalog&gt;&lt;book id="bk101"&gt;&lt;author&gt;Gambardella, Matthew&lt;/author&gt;&lt;title&gt;XML Developer\'s Guide&lt;/title&gt;&lt;genre&gt;Computer&lt;/genre&gt;&lt;price&gt;44.95&lt;/price&gt;&lt;publish_date&gt;2000-10-01&lt;/publish_date&gt;&lt;description&gt;An in-depth look at creating applications with XML.&lt;/description&gt;&lt;/book&gt;&lt;/catalog&gt;';

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

Output:

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

Converts an XML value to a string.
{#r_XUJ-valueToString_S__table_ix2_4hs_pt__entry__3}

| Name | Type | Description |
|-|-|-|
| XMLvalue | String | The XML to convert |
[Table 7. Parameters]

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

| Type | Description |
|-|-|
| String | The XML value converted to a string. |
[Table 8. Returns]

{#r_XUJ-valueToString_S__table_jx2_4hs_pt}  
The following example shows how to convert an XML document to a string.

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

Output:

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;catalog&gt;&lt;book id="bk101"/&gt;&lt;author&gt;Gambardella, Matthew&lt;/author&gt;&lt;title&gt;XML Developer&lt;/title&gt;&lt;genre&gt;Computer&lt;/genre&gt;&lt;price&gt;44.95&lt;/price&gt;&lt;/catalog&gt;


