Count occurences of a specific node in XML file
I want to read this XML file in JavaScript function and count the D开发者_JAVA技巧epartment nodes, can anybody help me out?
XML file:
<Departments>
<Department>
<name>name1</name>
<code>code1</code>
</Department>
<Department>
<name>name2</name>
<code>code2</code>
</Department>
<Department>
<name>name3</name>
<code>code3</code>
</Department>
</Departments>
How about
xmldoc.getElementsByTagName("Department").length;
If you need some quick code to get the xml:
var xmlHttp =(window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject('Microsoft.XMLHTTP');
var url = "departments.xml";
xmlHttp.open('GET', url, false); // syncronous
xmlHttp.send();
var xmlDoc = xmlHttp.responseXML;
using E4X in Javascript, Departments.Department.length();
Just count how many times "<Department>"
occurs in the string.
精彩评论