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-12-2016 01:21 PM
Hi Jesus,
Here goes a very quick example using regex (disclaimer: i did this very quickly. It can perhaps be improved in many ways but I just want to provide to you an initial timely help to get you on the right track)
I hope it helps and that you're doing great!
var xmlString ="<l:cellphone><name>data_type</name><value>1X</value></l:cellphone><l:cellphone><name>sms</name><value>none</value></l:cellphone>";
var reg = new SNC.Regex('/<name>([(.*)^[^<]]+)/');
var match = reg.match(xmlString);
var count =0;
var matches = [];
while (match != null) {
matches.push(match);
match = reg.match();
gs.log('match[' +count+ '] is ' + matches[count].toString().split(',')[1]);
count++;
}
gs.print('count is ' + count);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2016 01:22 PM
Output:
*** Script: match[0] is data_type
*** Script: match[1] is sms
*** Script: count is 2
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2016 01:22 PM
Please note I'm just using a subset of you xmlString string just for testing purposes.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2016 08:22 PM
I have the following at the top of the script include:
var response = s.execute();
var responseBody = response.getBody();
var status = response.getStatusCode();
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);
//here my code using regex
getNameFields();
function getNameFields() {
SNC.Regex('/<name>([(.*)^[^<]]+)/');
var match = reg.match(xmlDoc);
var count =0;
var matches = [];
while (match != null) {
matches.push(match);
match = reg.match();
gs.log('match[' +count+ '] is ' + matches[count]);
count++;
}
gs.info('jj count is' + count);
}
When including this code no output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2016 09:56 PM
Hi Jesus,
Here goes some comments:
a) I believe your xml string will be within the responseBody. Please check that contains your xml string. You can do so by doing a gs.log(responseBody) and checking under System Logs >> System Log >> Script Log Statements.
b) Please note that the matches is the array which contains all the values for the name tag. The way it worked for me was on the following:
matches[i].toString().split(',')[1] // i is the index of the match for which you want the value
Thanks,
Berny