I need to parse the below xml in javascript.
<Department id='1' name='Admins'>
<Floor Id='5' Name='WingA'>
<Employee Id='35' Name='John Smith' SysId='120' FileId='135' />
<Employee Id='124' Name='John Doe' SysId='214' FileId='125' />
<Employee Id='79' Name='Lorem Ipsum' SysId='185' FileId='194' />
</Floor>
</Department>
What I need is to loop through all the employees until I met a given condition (for example, get FileId of the employee node where SysId=214). I am able to get the Floor node but not sure how to iterate through the children and match the condition? The childNodes[0].nodeValue doesnt seem to work
parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlstr, "text/xml");
floor = xmlDoc.getElementsByTagName("Floor");
for (i = 0; i < floor.length; i++) {
floor[i].childNodes[0].nodeValue
}
Try something like this:
Test Fiddle: https://jsfiddle.net/1r2ydxhu/