admin管理员组文章数量:1406312
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.
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.
Share Improve this question asked Jul 31, 2012 at 19:48 FonzyFonzy 2011 gold badge4 silver badges14 bronze badges1 Answer
Reset to default 4try 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 :)
本文标签: Javascript Xml CountStack Overflow
版权声明:本文标题:Javascript Xml Count - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745005595a2637241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论