Reputation: 201
Sample XML.
<node>
<nodeid>28</nodeid>
<account_no xsi:nil="true" />
<address1>15 CANCUN CT</address1>
<serial_no>112199543</serial_no>
<x_lat>25.95513358000</x_lat>
<y_lon>-97.49027147000</y_lon>
<alarm>
<alarmid>Outage</alarmid>
<alarmtime>2012-07-30T14:46:29</alarmtime>
</alarm>
<alarm>
<alarmid>Restore</alarmid>
<alarmtime>2012-07-30T14:55:29</alarmtime>
</alarm>
</node>
<node>
<nodeid>67</nodeid>
<account_no>274192</account_no>
<address1>1618 CHIPINQUE DR</address1>
<serial_no>112199521</serial_no>
<x_lat>25.95286395000</x_lat>
<y_lon>-97.49323166000</y_lon>
<alarm>
<alarmid>Outage</alarmid>
<alarmtime>2012-07-30T14:46:29</alarmtime>
</alarm>
</node>
</ROOT>
I want to count the number of
<alarm>
elements the first node has as well as the second. I tried to do this in a for loop...
xmlDoc.getElementByTagName('alarm')[i].length;
this gives me all number of 'alarm' tags in the xml file. Which all i want is the current 'node' alarm elements. So here is what I want, I want for it to tell me the first has 2
<alarm>
tags and the second 'node' has 1
<alarm>
Just javascript no jQuery.
Upvotes: 2
Views: 8064
Reputation: 13726
try this:
var nodes = xmlDoc.getElementsByTagName('node'), //Get the <node> tags
amountOfNodes = nodes.length
for(var i = 0; i < amountOfNodes; i++) { //loop thru the nodes
console.log(nodes[i].getElementsByTagName('alarm').length); //get the amount of <alarm> tags
}
EDIT Here's a working example: Fiddle.
Tell me if that solves your problem :)
Upvotes: 4