XMLDocument2 - Scoped, Global
-
- UpdatedJan 30, 2025
- 6 minutes to read
- Yokohama
- API reference
The XMLDocument2 API is a JavaScript object wrapper for parsing and extracting XML data from an XML string.
Use this JavaScript class to create an object from an XML string, usually a return value from a web-service invocation, or the XML payload of ECC Queue. Using the XMLDocument2 object in a JavaScript business rule lets you query values from the XML elements and attributes directly.
An XML string has a tree structure, and the parts of the structure are called nodes. An XMLDocument2 object deals with two node types, element, and document element. An element node is a node with a name and possibly attributes and child nodes. A document-element node is the root node of the XML tree. It is the only node without a parent node.
Scoped XMLDocument2 - XMLDocument2()
Creates an XMLDocument2 object.
Name | Type | Description |
---|---|---|
None |
Scoped XMLDocument2 - XMLDocument2( GlideScriptableInputStream inputStream)
Creates an XMLDocument2 object from an attachment stream.
Name | Type | Description |
---|---|---|
inputStream | GlideScriptableInputStream | The input stream the XMLDocument2 object encapsulates. |
Scoped XMLDocument2 - createElement(String name)
Creates and adds an element node to the current node. The element name is the string passed in as a parameter. The new element has no text child nodes.
Name | Type | Description |
---|---|---|
name | String | The new element's name. |
Type | Description |
---|---|
XMLNode | Current XML node. |
Example
Scoped XMLDocument2 - createElementWithTextValue(String name, String value)
Creates and adds an element node with a text child node to the current node.
Name | Type | Description |
---|---|---|
name | String | Name of the element to add. |
value | String | Element's text value. |
Type | Description |
---|---|
XMLNode | Current XML node. |
Example
Scoped XMLDocument2 - getDocumentElement()
Gets the document element node of the XMLdocument2 object. The document element node is the root node.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
XMLNode | The document element. |
Example
Scoped XMLDocument2 - getFirstNode(String xPath)
Gets the first node in the specified xPath.
To work around this, use xPath without predicates, such as
"/store/resources/resource[@type='bookstore']/book")
and then filter the
nodes in the script using the getFirstNode() and getNextNode()
methods.
Name | Type | Description |
---|---|---|
xPath | String | The xPath to obtain the node from. |
Type | Description |
---|---|
XMLNode | The first node. |
Example
Scoped XMLDocument2 - getNextNode(Object current)
Gets the node after the specified node.
Name | Type | Description |
---|---|---|
current | Object | The current node. |
Type | Description |
---|---|
XMLNode | The next node. |
Example
Scoped XMLDocument2 - getNode(String xPath)
Gets the node specified in the xPath.
Name | Type | Description |
---|---|---|
xPath | String | xPath of the node to obtain. |
Type | Description |
---|---|
XMLNode | Current XML node. |
Example
Scoped XMLDocument2 - getNodeText(String xPath)
Gets all the text child nodes from the node referenced in the specified XPath.
Name | Type | Description |
---|---|---|
xPath | String | XPath of the text to obtain. |
Type | Description |
---|---|
String | Text children in the XPath. |
Example
Output:
Scoped XMLDocument2 - parseXML(String xmlDoc)
Parses the XML string and loads it into the XMLDocument2 object.
Name | Type | Description |
---|---|---|
xmlDoc | String | The document to parse. |
Type | Description |
---|---|
Boolean | Flag that indicates whether the content was parsed. |
Example
This example parses the xmlString and loads it into the xmlDocument2 object.
Scoped XMLDocument2 - setCurrentElement(XMLNode element)
Makes the node passed in as a parameter the current node.
Name | Type | Description |
---|---|---|
element | XMLNode | The element node to set as the current node. |
Type | Description |
---|---|
void |
Example
Scoped XMLDocument2 - setEnableCDATAReporting(Boolean enable)
Sets whether nodes are treated as CDATA or regular text after parsing. CDATA reporting is deactivated by default.
This method must be called with Scoped XMLDocument2 - parseXML(String xmlDoc).
See also: Scoped XMLNode - isCDATANode().
Name | Type | Description |
---|---|---|
enable | Boolean | Flag that indicates whether to handle CDATA nodes as CDATA or regular text nodes. Valid values:
Default: False |
Type | Description |
---|---|
None |
Example
The following example shows how CDATA is parsed in an element with CDATA reporting activated. The result of content.getFirstChild() is an element that holds the text value another. This element internally contains the information identifying it as a CDATA type. The result of content.getLastChild() is an element that holds the text value element. This element internally contains the information as a text type.
Output:
Scoped XMLDocument2 - setNamespaceAware(Boolean aware)
When set to true, the XMLDocument2 object processes the document with XML namespaces.
If you don't set this, an XML document with namespaces won't be enumerated correctly, and an XPath search would fail.
Name | Type | Description |
---|---|---|
aware | Boolean | When true, the XMLDocument2 object processes the document with XML namespaces. |
Type | Description |
---|---|
void |
Scoped XMLDocument2 - toString()
Returns a string containing the XML.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
String | A string containing the XML. |
Example
On this page
- Scoped XMLDocument2 - XMLDocument2()
- Scoped XMLDocument2 - XMLDocument2( GlideScriptableInputStream inputStream)
- Scoped XMLDocument2 - createElement(String name)
- Scoped XMLDocument2 - createElementWithTextValue(String name, String value)
- Scoped XMLDocument2 - getDocumentElement()
- Scoped XMLDocument2 - getFirstNode(String xPath)
- Scoped XMLDocument2 - getNextNode(Object current)
- Scoped XMLDocument2 - getNode(String xPath)
- Scoped XMLDocument2 - getNodeText(String xPath)
- Scoped XMLDocument2 - parseXML(String xmlDoc)
- Scoped XMLDocument2 - setCurrentElement(XMLNode element)
- Scoped XMLDocument2 - setEnableCDATAReporting(Boolean enable)
- Scoped XMLDocument2 - setNamespaceAware(Boolean aware)
- Scoped XMLDocument2 - toString()