{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1135,"path":"api-reference/server-api-reference/c_XMLUtilJSAPI.md","title":"XMLUtilJS - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# XMLUtilJS- Global\n\nThe XMLUtilJS script include provides XML utility methods for JavaScript to be used with Discovery scripts.\n\nThis script include is provided with the Discovery plugin. Use this script include in any server-side Discovery script in which you need XML utilities.\n\nAccess these methods using the static variable XMLUtilJS.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## XMLUtilJS - escapeForXMLText\\(String text\\)\n\nProvides escape text for a given string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|The text to format.|\n\n|Type|Description|\n|----|-----------|\n|String|The formatted text.|\n\nThe following example shows how to list escaped XML data in a document. See also stringToValue\\(\\).\n\n```\n// Create an XML document to work with\nvar doc = new XMLDocument2();\n\n// Add an element called catalog\nvar catalog = doc.createElement('catalog');\n\n// Set current element to the one previously made to add more elements\ndoc.setCurrentElement(catalog);\n\n// Add an element called catalog\nvar bookAttribute = doc.createElement('book');\n\n// Add an attribute to the element created\nbookAttribute.setAttribute('id', 'bk101');\n\n// Create multiple elements with a defined value\ndoc.createElementWithTextValue('author', 'Gambardella, Matthew');\ndoc.createElementWithTextValue('title', 'XML Developer');\ndoc.createElementWithTextValue('genre', 'Computer');\ndoc.createElementWithTextValue('price', '44.95');\n\n// Pass the created XML document as a string to the XMLUtilJS API function\nvar escapedXML = XMLUtilJS.escapeForXMLText(doc.toString());\n\n// Print the escapedXML. If an invalid XML is created, the original value will be returned\ngs.print(escapedXML);\n```\n\nOutput:\n\n```\n&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;\n```\n\n## XMLUtilJS - stringToValue\\(String str\\)\n\nConverts a string to an XML value.\n\n|Name|Type|Description|\n|----|----|-----------|\n|str|String|The string to convert.|\n\n|Type|Description|\n|----|-----------|\n|String|The specified string converted to XML.|\n\nThe following example shows how to transform a string of escaped XML to tagged XML output. See also escapeForXMLText\\(\\).\n\n```\n//Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()\nvar 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;';\n\n//Pass in escaped XML text and store output in a variable\nvar output = XMLUtilJS.stringToValue(escapedXML);\n\n// If the input value is a string value of NULL, the output will be null, otherwise an unescaped XML string value.\ngs.print(output);\n```\n\nOutput:\n\n```\n<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>\n```\n\n## XMLUtilJS - unescapeForXMLText\\(String text\\)\n\nProvides un-escaped XML for a given string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|The XML text to clean up.|\n\n|Type|Description|\n|----|-----------|\n|String|The cleaned up XML string.|\n\nThe following example shows how to convert an escaped XML string to tagged XML output. See also escapeForXMLText\\(\\).\n\n```\n//Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()\nvar 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;';\n\n//Pass in escaped XML text and store output in a variable\nvar output = XMLUtilJS.unescapeForXMLText(escapedXML);\n\n\n//Print the escapedXML. If the XML is invalid, the original value will be returned\ngs.print(output);\n```\n\nOutput:\n\n```\n<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>\n\n```\n\n## XMLUtilJS - valueToString\\(String XMLvalue\\)\n\nConverts an XML value to a string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|XMLvalue|String|The XML to convert|\n\n|Type|Description|\n|----|-----------|\n|String|The XML value converted to a string.|\n\nThe following example shows how to convert an XML document to a string.\n\n```\n//Create an XML document to work with\nvar doc = new XMLDocument2();\n\n//Add an element called catalog\nvar catalog = doc.createElement(\"catalog\");\n\n//Set our current element to the one previously made to add further elements\ndoc.setCurrentElement(catalog);\n\n//Add an element called catalog\nvar bookAttribute = doc.createElement(\"book\");\n\n//Add an attribute to the element created\nbookAttribute.setAttribute(\"id\" , \"bk101\");\n\n//create multiple elements with a defined value\ndoc.createElementWithTextValue(\"author\" , \"Gambardella, Matthew\");\ndoc.createElementWithTextValue(\"title\" , \"XML Developer\");\ndoc.createElementWithTextValue(\"genre\" , \"Computer\");\ndoc.createElementWithTextValue(\"price\" , \"44.95\");\n\n//Pass the created XML document as a string to the XMLUtilJS API function.\nvar escapedXML = XMLUtilJS.valueToString(doc.toString());\n\n// Print the results\n// Returns NULL if XML is invalid\ngs.print(escapedXML);\n```\n\nOutput:\n\n```\n&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;\n```\n\n"}}