XMLDocument script object
Summarize
Summary of XMLDocument script object
The XMLDocument script object in ServiceNow provides a JavaScript wrapper to parse and extract XML data from XML strings, commonly returned by Web Service invocations or ECC Queue payloads. It allows you to create an XMLDocument instance from an XML string and query XML elements and attributes directly in server-side scripts like Business Rules. This object supports namespace-aware parsing by passing a second argument to its constructor.
Show less
Key Features
- Constructor: Instantiate an XMLDocument object from an XML string, optionally enabling namespace awareness for XML with namespaces.
- XPath-based node querying: Use XPath strings to locate nodes and extract text or integer values with methods like
getNodeText()andgetNodeInt(). - Node and attribute access: Retrieve nodes by XPath and access node properties such as name, type, and child nodes. Attributes can be accessed either directly or via XPath queries.
- Node lists: Obtain collections of nodes matching XPath expressions and iterate through or access nodes by index.
- Creating new elements: Add new XML elements dynamically at any position in the document using
createElement(). UsesetCurrent()to define the parent element for subsequent creations. - Namespace handling: When parsing XML with namespaces, enable namespace-aware mode to query elements using their namespace prefixes in XPath expressions.
Practical Usage for ServiceNow Customers
- Parse XML payloads received from web services or ECC Queue messages efficiently within server-side scripts.
- Extract specific data points from XML responses using XPath queries to support integrations and automation workflows.
- Access and manipulate XML elements and attributes programmatically, enabling dynamic XML document modifications.
- Work with namespace-qualified XML documents seamlessly by enabling the namespace-aware parsing mode.
Key Outcomes
- Enable robust XML data handling within ServiceNow scripts without external libraries.
- Improve integration capabilities by simplifying XML parsing, querying, and document editing tasks.
- Support complex XML structures including those with namespaces, facilitating richer data exchange scenarios.
- Empower developers to build custom logic that interacts directly with XML data in a performant and straightforward way.
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>