XMLDocument2 - Scoped

XMLDocument2 is a JavaScript Object wrapper for parsing and extracting XML data from an XML 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 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 - 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.

Table 1. Parameters
Name Type Description
name String The new element's name.
Table 2. Returns
Type Description
void
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 XMLDocument2();
xmlDoc.parseXML(xmlString); 
xmlDoc.createElement("new2"); 
 
gs.info(xmlDoc);
Output:
?xml version="1.0" encoding="UTF-8"?>
<test>  
    <one>    
        <two att="xxx">abcd1234</two>    
            <three att="yyy" boo="yah">1234abcd</three>    
        <two>another</two>  
    </one>  
    <number>1234</number>
    <new2></new2>
</test>

Scoped XMLDocument2 - createElementWithTextValue(String name, String value)

Creates and adds an element node with a text child node to the current node.

Table 3. Parameters
Name Type Description
name String Name of the element to be added.
value String The added element's text value.
Table 4. Returns
Type Description
XMLNode The current node.
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 XMLDocument2();
xmlDoc.parseXML(xmlString); 
xmlDoc.createElement("new", "test");
gs.info(xmlDoc);
Output:
<?xml version="1.0" encoding="UTF-8"?>
    <test>
        <one>
            <two att="xxx">abcd1234</two>    
            <three att="yyy" boo="yah">1234abcd</three>    
            <two>another</two>  
        </one>  
        <number>1234</number>
        <new>test</new>
    </test>

Scoped XMLDocument2 - getDocumentElement()

Gets the document element node of the XMLdocument2 object. The document element node is the root node.

Table 5. Parameters
Name Type Description
None
Table 6. Returns
Type Description
XMLNode The document element.
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 XMLDocument2();
xmlDoc.parseXML(xmlString);
//returns the root node of the document tree.
var rootNode = xmlDoc.getDocumentElement();
gs.info(rootNode.getTextContent());
Output:
abcd1234    1234abcd    another    1234

Scoped XMLDocument2 - getFirstNode(String xPath)

Gets the first node in the specified XPATH.

Table 7. Parameters
Name Type Description
xPath String The XPATH.
Table 8. Returns
Type Description
XMLNode The first node.
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 XMLDocument2();
xmlDoc.parseXML(xmlString);
var foo = xmlDoc.getFirstNode('/test/one/two');
gs.info(foo.getTextContent());
Output:
abcd1234

Scoped XMLDocument2 - getNextNode(Object current)

Gets the node after the specified node.

Table 9. Parameters
Name Type Description
current Object The current node.
Table 10. Returns
Type Description
XMLNode The next node.
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 XMLDocument2();
xmlDoc.parseXML(xmlString);
var foo = xmlDoc. getFirstNode('/test/one/two');
var foo2 = xmlDoc.getNextNode(foo);
gs.info(foo.getTextContent());
gs.info(foo2.getTextContent());
Output:
abcd1234

another

Scoped XMLDocument2 - getNode(String xPath)

Gets the node specified in the xpath.

Table 11. Parameters
Name Type Description
xPath String The xpath
Table 12. Returns
Type Description
XMLNode The current node.
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 XMLDocument2();
xmlDoc.parseXML(xmlString);
var node = xmlDoc.getNode("/test/one/two");
gs.info(node);
Output:
<two att="xxx">abcd1234</two>

Scoped XMLDocument2 - getNodeText(String xPath)

Gets all the text child nodes from the node referenced in the xpath.

Table 13. Parameters
Name Type Description
xPath String the xpath.
Table 14. Returns
Type Description
XMLNode The text children in the xpath.
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 XMLDocument2();
xmlDoc.parseXML(xmlString); 
gs.info(xmlDoc.getNodeText("//two"));

Output: abcd1234

Scoped XMLDocument2 - parseXML(String xmlDoc)

Parses the XML string and loads it into the XMLDocument2 object.

Table 15. Parameters
Name Type Description
xmlDoc String The document to parse.
Table 16. Returns
Type Description
void

This example parses the xmlString and loads it into the xmlDocument2 object.

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 XMLDocument2();
xmlDoc.parseXML(xmlString); 
var rootNode = xmlDoc.getDocumentElement();

Output:

Scoped XMLDocument2 - setCurrentElement(XMLNode element)

Makes the node passed in as a parameter the current node.

Table 17. Parameters
Name Type Description
element XMLNode The element node to set as the current node.
Table 18. Returns
Type Description
void
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 XMLDocument2();
xmlDoc.parseXML(xmlString);
//returns the root node of the document tree.
var rootNode = xmlDoc.getDocumentElement(); //returns org.w3c.dom.Element
// sets the root node as the current element
xmlDoc.setCurrentElement(rootNode);

Output:

Scoped XMLDocument2 - toString()

Returns a string containing the XML.

Table 19. Parameters
Name Type Description
None
Table 20. Returns
Type Description
String A string containing the XML.
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 XMLDocument2();
xmlDoc.parseXML(xmlString);
gs.info(xmlDoc.toString());
Output - Line breaks were added to the output for readability.
<?xml version="1.0" encoding="UTF-8"?>
<test>  
<one>    
<two att="xxx">abcd1234</two>    
<three att="yyy" boo="yah">1234abcd</three>    
<two>another</two>  
</one>  
<number>1234</number>
</test>

Scoped XMLDocument2 - XMLDocument2()

Instantiates an XMLDocument2 object.

Table 21. Parameters
Name Type Description
None

      

Output: