XMLDocument script object
Summarize
Summary of XMLDocument script object
The XMLDocument script object in ServiceNow provides a JavaScript wrapper to parse and extract data from XML documents represented as strings. It is commonly used to handle XML payloads such as those returned from Web Service calls or the ECC Queue. This object allows you to query XML elements and attributes directly within JavaScript business rules or other server-side scripts.
Show less
Key Features
- Instantiation: Create an XMLDocument instance by passing an XML string. Optionally, pass
trueas the second argument to enable namespace-aware parsing for XML with qualified namespaces. - XPath Querying: Use XPath syntax to locate nodes and attributes within the XML document. Functions such as
getNodeText(),getNodeInt(),getNode(), andgetNodes()facilitate extracting text, integers, node objects, or lists of nodes. - Node Inspection: Retrieve node details like name (
getNodeName()) and type (getNodeType()), or access node children and their values. - Attribute Access: Attributes can be accessed via node objects or directly queried by XPath. The
getAttribute()function retrieves attribute values by name. - Element Creation: Dynamically create new XML elements at any point in the document. Use
setCurrent()to define the parent node for new elements andcreateElement()to add elements with optional text content. - Namespace Handling: When parsing XML with namespaces, enable namespace awareness to properly query elements with prefixes.
Practical Application for ServiceNow Customers
By leveraging the XMLDocument object, ServiceNow developers can efficiently parse complex XML data within server-side scripts, enabling extraction and manipulation of XML payloads from integrations and web services. This improves automation workflows by allowing direct access to XML elements and attributes without external parsing tools.
Key actions such as reading values, iterating nodes, querying attributes, and dynamically modifying XML structures become straightforward, helping customers customize data processing and integration logic in their ServiceNow instances.
Additional Tools
The XMLHelper script include complements the XMLDocument object by simplifying XML parsing tasks in scripts, making it easier to work with XML data in common scenarios.
A JavaScript object wrapper for parsing and extracting XML data from an XML document (String).
Use this Javascript class to instantiate an object from an XML string, usually a return value from a Web Service invocation, or the XML payload of ECC Queue. Using the XMLDocument object in a Javascript business rule lets you query values from the XML elements and attributes directly.
Constructor
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmldoc = new XMLDocument(xmlString);var xmlString = "<bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>" +
"<bk:title>Cheaper by the Dozen</bk:title>" +
"<isbn:number>1568491379</isbn:number>" +
"</bk:book>";
var xmldoc = new XMLDocument(xmlString, true); // XML document is name space aware
Locating nodes and elements
var str = xmldoc.getNodeText("//two"); // returns the first occurrence of the node
// str == "abcd1234"
str = xmldoc.getNodeText("//three");
// str == "1234abcd"
str = xmldoc.getNodeText("/test/one/*");
// str == "abcd1234"
str = xmldoc.getNodeInt("//number");
// str == 1234
var node = xmldoc.getNode("/test/one/two");
// node.getNodeName() == "two"
// node.getNodeType() == "1" // 1 == ELEMENT_NODE
// node.getLastChild().getNodeType() == "3" // 3 == TEXT_NODE
// node.getLastChild().getNodeValue() == "abcd1234"str = xmldoc.getNodeName("//three");
// str == "three"
str = xmldoc.getNodeType("//three");
// str == "1"var nodelist = xmldoc.getNodes("//one/*"); // two, three, and two
// nodelist.getLength() == "3"
// nodelist.item(0).getNodeName() == "two"
// nodelist.item(1).getNodeName() == "three"var xmlString = "<bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>" +
"<bk:title>Cheaper by the Dozen</bk:title>" +
"<isbn:number>1568491379</isbn:number>" +
"</bk:book>";
var xmldoc = new XMLDocument(xmlString, true);
var str = xmldoc.getNodeText("//bk:title"); // returns the first occurence of the node
gs.log(str);
str = xmldoc.getNodeText("/bk:book/*");
gs.log(str);
str = xmldoc.getNodeInt("//isbn:number");
gs.log(str);Getting attribute values
An attribute is just an extension of a node and so it has all the same APIs.
node = xmldoc.getNode("//two");
// node.getAttributes().item(0).getNodeValue() == "xxx"
str = xmldoc.getAttribute("//two", "att");
// str == "xxx"str = xmldoc.getNodeText("//*[@att=\"yyy\"]");
// str == "1234abcd"
str = xmldoc.getNode("//@boo").getNodeValue();
// str == "yah"
Creating new elements
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmldoc = new XMLDocument(xmlString);
xmldoc.createElement("new", "test"); // creates the new element at the document element level if setCurrent is never called
xmldoc.createElement("new2"); // calling without a value will create a new element by itself
var el = xmldoc.createElement("new3");
xmldoc.setCurrent(el); // this is now the parent of any new elements created subsequently using createElement()
xmldoc.createElement("newChild", "test");<test>
<one>
<two att="xxx">abcd1234</two>
<three boo="yah" att="yyy">1234abcd</three>
<two>another</two>
</one>
<number>1234</number>
<new>test<new>
<new2/>
<new3>
<newChild>test</newChild>
</new3>
</test>