How to get elements by tag name in an XML?

jesusemelendezm
Mega Guru

Hi

I have the following piece of XML and trying to parse it. I am using a client callable script include.

I need to get all the elements that are tagged with "<name>".

<l:cellphone>

<name>data_type</name>

<value>1X</value>

</l:cellphone>

<l:cellphone>

<name>sms</name>

<value>none</value>

</l:cellphone>

<l:cellphone>

<name>voice</name>

<value>none</value>

</l:cellphone>

<l:cellphone>

<name>wifi</name>

<value>wifi.net</value>

</l:cellphone>

I have tried the following 'getElementByTagName' but the script doesn't even run when that line is in. I have parsed other elements with getNodeText and that works but will only get the first element that is tagged with 'name'.

x = xmlDoc.getElementsByTagName("name")[0];// when this is in script stop running at that line and no getting anything on logs.

XML DOM - Get Node Values

Thanks for your help

10 REPLIES 10

ryanwarren
Giga Contributor

Jesus,



I pulled this function from one of ServiceNow's script includes (so no credit goes to me) but I believe you can strip out some of it's parts and get it to work for what you need:



function UpdateXMLPayloadParser(xmlString) {


      var doc = new XMLDocument2();


      doc.parseXML(xmlString);


      var recordNode = doc.getNode('record_update').getFirstChild();




      // sys_documentation records have an extra node,


      // also named sys_documentation, because why not


      if (recordNode.getNodeName() === 'sys_documentation')


              recordNode = recordNode.getFirstChild();




      var recordIterator = recordNode.getChildNodeIterator();




      var result = {},


              n;


      while (recordIterator.hasNext()) {


              n = recordIterator.next();


              result[n.getNodeName()] = n.getTextContent();


      }




      return result;


}


I continue having same roadblock... in this script, I am able to get the childnodes. But in my case... I have all the nodes at same level, they are not child to another. They are all parent nodes with same name. I need to iterate in a way that I can query all the elements   where getNodeText (//name) and add them to an array.



just like in here below... all thats tag with name is what I need to access... there is no child nodes as each of them are separated acting as parent nodes.




<l:cellphone>


<name>data_type</name>


<value>1X</value>


</l:cellphone>


<l:cellphone>


<name>sms</name>


<value>none</value>


</l:cellphone>


<l:cellphone>


<name>voice</name>


<value>none</value>


</l:cellphone>


<l:cellphone>


<name>wifi</name>


<value>wifi.net</value>


</l:cellphone>



In HTML type document I can do this with a for loop as follows... I am looking to translate this to ServiceNow.



var doc = document.getElementsByTagName('name');
for (var i = 0; doc[i]; i++) {
   
...


        //here I can build the array containing all elements with tag 'name'
}


ctomasi can provide me some help here?


bernyalvarado
Mega Sage

Hi Jesus,



On a very quick first sight to this I believe you can solve this by using regex on your xml incoming string.



Thanks,


Berny