How to get elements by tag name in an XML?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2016 02:45 PM
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.
Thanks for your help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-11-2016 03:17 PM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-11-2016 09:31 PM
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'
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2016 12:21 PM
ctomasi can provide me some help here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2016 01:05 PM
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