---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# XMLNode - Scoped, Global

# XMLNode - Scoped, Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 4 minutes to read

The XMLNode API provides methods 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](https://servicenow-prod.fluidtopics.net/YPBJ89gVrpIarVZ9NzmJgA#c_XMLDocument2ScopedAPI "The XMLDocument2 API is a JavaScript object wrapper for parsing and extracting XML data from an XML string."), which adds a node to an existing document.

## Scoped XMLNode - getAttribute(String attribute) {#ariaid-title2}

Gets the value of the attribute.
{#r_ScopedXMLNodeGetAttribute_String__table_ir2_12p_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| attribute | String | Name of the attribute. |
[Table 1. Parameters]

{#r_ScopedXMLNodeGetAttribute_String__table_ir2_12p_1r} {#r_ScopedXMLNodeGetAttribute_String__table_kr2_12p_1r__entry__2}

| Type | Description |
|-|-|
| String | The attribute's value. |
[Table 2. Returns]

{#r_ScopedXMLNodeGetAttribute_String__table_kr2_12p_1r}  

    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 - getAttributes() {#ariaid-title3}

Returns an object containing the node's attributes as properties with
values.
{#r_SXMLN_getAttributes__table_bk3_rjy_3x__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 3. Parameters]

{#r_SXMLN_getAttributes__table_bk3_rjy_3x} {#r_SXMLN_getAttributes__table_ck3_rjy_3x__entry__2}

| Type | Description |
|-|-|
| Object | Contains name-value pairs where the name is the attribute and the value is the attribute's value. |
[Table 4. Returns]

{#r_SXMLN_getAttributes__table_ck3_rjy_3x}

## Scoped XMLNode - getChildNodeIterator() {#ariaid-title4}

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

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 5. Parameters]

{#r_ScopedXMLNodeGetChildNodeIterator__table_ng3_xfc_2r} {#r_ScopedXMLNodeGetChildNodeIterator__table_brm_x2p_1r__entry__2}

| Type | Description |
|-|-|
| XMLNodeIterator | The node iterator object. |
[Table 6. Returns]

{#r_ScopedXMLNodeGetChildNodeIterator__table_brm_x2p_1r}  

    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() {#ariaid-title5}

Gets the node's first child node.
{#r_ScopedXMLNodeGetFirstChild__table_wbb_wfc_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 7. Parameters]

{#r_ScopedXMLNodeGetFirstChild__table_wbb_wfc_2r} {#r_ScopedXMLNodeGetFirstChild__table_ecg_ccp_1r__entry__2}

| Type | Description |
|-|-|
| XMLNode | The node's first child node. |
[Table 8. Returns]

{#r_ScopedXMLNodeGetFirstChild__table_ecg_ccp_1r}  

    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() {#ariaid-title6}

Gets the node's last child node.
{#r_ScopedXMLNodeGetLastChild__table_gw2_vfc_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 9. Parameters]

{#r_ScopedXMLNodeGetLastChild__table_gw2_vfc_2r} {#r_ScopedXMLNodeGetLastChild__table_kgc_pbp_1r__entry__2}

| Type | Description |
|-|-|
| XMLNode | The node's last child. |
[Table 10. Returns]

{#r_ScopedXMLNodeGetLastChild__table_kgc_pbp_1r}  

    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() {#ariaid-title7}

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.
{#r_ScopedXMLNodeGetNodeName__table_oxk_vfc_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 11. Parameters]

{#r_ScopedXMLNodeGetNodeName__table_oxk_vfc_2r} {#r_ScopedXMLNodeGetNodeName__table_ftf_1dp_1r__entry__2}

| Type | Description |
|-|-|
| String | The node's name. |
[Table 12. Returns]

{#r_ScopedXMLNodeGetNodeName__table_ftf_1dp_1r}  

    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() {#ariaid-title8}

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

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 13. Parameters]

{#r_ScopedXMLNodeGetNodeValue__table_jbm_tfc_2r} {#r_ScopedXMLNodeGetNodeValue__table_djn_4cp_1r__entry__2}

| Type | Description |
|-|-|
| String | The node's value. |
[Table 14. Returns]

{#r_ScopedXMLNodeGetNodeValue__table_djn_4cp_1r}  

    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() {#ariaid-title9}

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

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 15. Parameters]

{#r_ScopedXMLNodeGetTextContent__table_g21_tfc_2r} {#r_ScopedXMLNodeGetTextContent__table_igr_z1p_1r__entry__2}

| Type | Description |
|-|-|
| String | The text content of the current node. |
[Table 16. Returns]

{#r_ScopedXMLNodeGetTextContent__table_igr_z1p_1r}  

    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) {#ariaid-title10}

Determines if the node has the specified attribute.
{#r_ScopedXMLNodeHasAttribute_String__table_art_mdp_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| attribute | String | The name of the attribute to check. |
[Table 17. Parameters]

{#r_ScopedXMLNodeHasAttribute_String__table_art_mdp_1r} {#r_ScopedXMLNodeHasAttribute_String__table_crt_mdp_1r__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the node has the attribute. |
[Table 18. Returns]

{#r_ScopedXMLNodeHasAttribute_String__table_crt_mdp_1r}  

    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 - isCDATANode() {#ariaid-title11}

Indicates whether the CDATA node is preserved as a separate node.
Use the [Scoped XMLDocument2 - setEnableCDATAReporting(Boolean enable)](https://servicenow-prod.fluidtopics.net/YPBJ89gVrpIarVZ9NzmJgA#SXMLD_setEnableCDATAReporting_B "Sets whether nodes are treated as CDATA or regular text after parsing. CDATA reporting is deactivated by default.") method to ensure that CDATA nodes are preserved and not handled as text.
{#SXMLN_isCDATANode__table_bk3_rjy_3x__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 19. Parameters]

{#SXMLN_isCDATANode__table_bk3_rjy_3x} {#SXMLN_isCDATANode__table_ck3_rjy_3x__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that indicates whether a queried node is CDATA or plain text. Valid values: * true: The node queried is CDATA. * false: The node queried is plain text. {#SXMLN_isCDATANode__ul_fj4_svh_lzb} |
[Table 20. Returns]

{#SXMLN_isCDATANode__table_ck3_rjy_3x}  
The following example shows how to parse an XML string with CDATA reporting enabled using [Scoped XMLDocument2 - setEnableCDATAReporting(Boolean enable)](https://servicenow-prod.fluidtopics.net/YPBJ89gVrpIarVZ9NzmJgA#SXMLD_setEnableCDATAReporting_B "Sets whether nodes are treated as CDATA or regular text after parsing. CDATA reporting is deactivated by default."). The code uses isCDATANode() to show that the first node queried in the XML string is a CDATA node.

    var xmlString = "<test>" +
      "  <one>" +
      "    <two att=\"xxx\">abcd1234</two>" +
      "    <three boo=\"yah\" att=\"yyy\">1234abcd</three>"+
      "    <four><![CDATA[another]]>element</four>" +
      "  </one>" +
      "  <number>1234</number>" +
      "</test>";
    var xmlDoc = new XMLDocument2();
    xmlDoc.setEnableCDATAReporting(true); // Enables CDATA reporting
    xmlDoc.parseXML(xmlString);
    var content = xmlDoc.getFirstNode('/test/one/four');
    gs.info(content.getFirstChild().isCDATANode());

Output:

    true

## Scoped XMLNode - toString() {#ariaid-title12}

Returns the string value of the current node.
{#r_ScopedXMLNodeToString__table_jjy_pfc_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 21. Parameters]

{#r_ScopedXMLNodeToString__table_jjy_pfc_2r} {#r_ScopedXMLNodeToString__table_bqm_nfp_1r__entry__2}

| Type | Description |
|-|-|
| String | The string value of the current node. |
[Table 22. Returns]

{#r_ScopedXMLNodeToString__table_bqm_nfp_1r}  

    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>


