XMLNode - Scoped

The scoped XMLNode API allows you to query values from XML nodes. XMLNodes are extracted from XMLDocument2 objects, which contain XML strings.

There are no constructors for creating a stand alone instance of an XMLNode object. Instead, use the createElement() method of XMLDocument2, which adds a node to an existing document.

Scoped XMLNode - getAttribute(String attribute)

Gets the value of the attribute.

Table 1. Parameters
Name Type Description
attribute String Name of the attribute.
Table 2. Returns
Type Description
String The attribute's value.
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('//two');
gs.info(node.getAttribute('att'));

Output: xxx

Scoped XMLNode - getChildNodeIterator()

Gets a XMLNodeIterator object that can be used to walk through the list of child nodes.

Table 3. Parameters
Name Type Description
None
Table 4. Returns
Type Description
XMLNodeIterator The node iterator 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 node = xmlDoc.getNode('//one');
var iter= node.getChildNodeIterator();
gs.info(iter.hasNext());

Scoped XMLNode - getFirstChild()

Gets the node's first child node.

Table 5. Parameters
Name Type Description
None
Table 6. Returns
Type Description
XMLNode The node's first child 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('//one');
gs.info(node.getFirstChild());
Output:
<two att="xxx">abcd1234</two>

Scoped XMLNode - getLastChild()

Gets the node's last child node.

Table 7. Parameters
Name Type Description
None
Table 8. Returns
Type Description
XMLNode The node's last child.
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('//one');
 
gs.info(node.getLastChild());
Output:
<two>another</two>

Scoped XMLNode - getNodeName()

Gets the node's name. A node's name is determined by the node type. A document-element node's name is #document. A text node's name is #text. An element node's name is the element's name.

Table 9. Parameters
Name Type Description
None
Table 10. Returns
Type Description
String The node's name.
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('//two');
gs.info(node.getNodeName());

Output: two

Scoped XMLNode - getNodeValue()

Gets the node's value. A node's value is determined by the node type. Element and document-element nodes return null.

Table 11. Parameters
Name Type Description
None
Table 12. Returns
Type Description
String The node's value.
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('//two');
gs.info(node.getNodeValue());

Output: null

Scoped XMLNode - getTextContent()

Gets the text content of the current node. The text content of a node consists of all the node's child text nodes

Table 13. Parameters
Name Type Description
None
Table 14. Returns
Type Description
String The text content of 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('//one/two');
gs.info(node.getTextContent());

Output: abcd1234

Scoped XMLNode - hasAttribute(String attribute)

Determines if the node has the specified attribute.

Table 15. Parameters
Name Type Description
attribute String The name of the attribute to check.
Table 16. Returns
Type Description
Boolean True if the node has the attribute.
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('//two');
gs.info(node.hasAttribute('att'));

Output: true

Scoped XMLNode - toString()

Returns the string value of the current node.

Table 17. Parameters
Name Type Description
None
Table 18. Returns
Type Description
String The string value of 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('//one');
gs.info(node.toString());
Output: Line breaks were added to the output.
<one>    
<two att="xxx">abcd1234</two>    
<three att="yyy" boo="yah">1234abcd</three>    
<two>another</two>  
</one>