I have an XML file (below) that I need to parse using jquery "**parseXML**".
The goal:
Traverse the hierarchy of XML file and loop over the iterations to extract the data.
- List in an array of all "mycase"
- for each "mycase" iteration, the list in an array of all "testrun"
- for each "testrun" iteration, the list in an array of all "router"
- for each "router" iteration, the list in an array of all "cmdname"
and "cmdfile"
Desired outcome:
The optimal outcome is to have each time the number of elements at each level, then loop over the number of iteration to show the content (ex: to the page) and continue doing the same at deeper level, etc...
Casename:- Case name
topofile:- file.jpg
Case id:- 1
--- testrun id:- 1
------ router id:- R1
--------- cmdname:- cmd111, cmdfile:- file111
--------- cmdname:- cmd112, cmdfile:- file112
------ router id:- R2
--------- cmdname:- cmd121, cmdfile:- file121
--- testrun id:- 2
------ router id:- R1
--------- cmdname:- cmd211, cmdfile:- file211
--------- cmdname:- cmd212, cmdfile:- file212
------ router id:- R2
--------- cmdname:- cmd221, cmdfile:- file221
------ router id:- R3
--------- cmdname:- cmd231, cmdfile:- file231
--------- cmdname:- cmd232, cmdfile:- file232
The issue:
I am using the below code and I could not hierarchically iterate over the elements. Tags are read globally not per children element.
For each testrun all routers are listed, even those belonging to other testruns.
For each router inside each testrun all commands are listed, even those belonging to other routers
XML content:
- <?xml version="1.0" ?>
- <lab>
- <mycase id="1">
- <casename>Case name</casename>
- <topo>file.jpg</topo>
- <testrun id="1">
- <router id="R1">
- <command>
- <cmdname>cmd111</cmdname>
- <cmdfile>file111</cmdfile>
- </command>
- <command>
- <cmdname>cmd112</cmdname>
- <cmdfile>file112</cmdfile>
- </command>
- </router>
- <router id="R2">
- <command>
- <cmdname>cmd121</cmdname>
- <cmdfile>file121</cmdfile>
- </command>
- </router>
- </testrun>
- <testrun id="2">
- <router id="R1">
- <command>
- <cmdname>cmd211</cmdname>
- <cmdfile>file211</cmdfile>
- </command>
- <command>
- <cmdname>cmd212</cmdname>
- <cmdfile>file212</cmdfile>
- </command>
- </router>
- <router id="R2">
- <command>
- <cmdname>cmd221</cmdname>
- <cmdfile>file221</cmdfile>
- </command>
- </router>
- <router id="R3">
- <command>
- <cmdname>cmd231</cmdname>
- <cmdfile>file231</cmdfile>
- </command>
- <command>
- <cmdname>cmd232</cmdname>
- <cmdfile>file232</cmdfile>
- </command>
- </router>
- </testrun>
- </mycase>
- </lab>
- var xml='<?xml version="1.0" ?><lab><mycase id="1"><casename>Case name</casename><topo>file.jpg</topo><testrun id="1"><router id="R1"><command><cmdname>cmd111</cmdname><cmdfile>file111</cmdfile></command><command><cmdname>cmd112</cmdname><cmdfile>file112</cmdfile></command></router><router id="R2"><command><cmdname>cmd121</cmdname><cmdfile>file121</cmdfile></command></router></testrun><testrun id="2"><router id="R1"><command><cmdname>cmd211</cmdname><cmdfile>file211</cmdfile></command><command><cmdname>cmd212</cmdname><cmdfile>file212</cmdfile></command></router><router id="R2"><command><cmdname>cmd221</cmdname><cmdfile>file221</cmdfile></command></router><router id="R3"><command><cmdname>cmd231</cmdname><cmdfile>file231</cmdfile></command><command><cmdname>cmd232</cmdname><cmdfile>file232</cmdfile></command></router></testrun></mycase></lab>';
- $(document).ready(function () {
- $xml = $( $.parseXML( xml ) );
- $xml.find("mycase").each(function(){
- $("#container").append(" Casename:- " + $(this).find("casename").text() + "<br />");
- $("#container").append(" topofile:- " + $(this).find("topo").text() + "<br />");
- $("#container").append(" Case id:- " + $(this).attr("id") + "<br />");
- $xml.find("testrun").each(function(){
- $("#container").append("--- testrun id:- " + $(this).attr("id") + "<br />");
- $xml.find("testrun").children("router").each(function(){
- $("#container").append("------ router id:- " + $(this).attr("id") + "<br />");
- $xml.find("command").each(function(){
- $("#container").append("--------- cmdname:- " + $(this).find("cmdname").text() + ", cmdfile:- " + $(this).find("cmdfile").text()+ "<br />");
- });
- });
- });
- });
- });
HTML part
Tried to use both below formats to get the specific children of an element, but without success:
Both lists all routers on the XML doc.
- $xml.find("mycase > testrun").each(function(){
- $("#container").append(" router:- " + $(this).attr('id') + "<br />");
- });
-
-
- $xml.find("mycase").children("testrun").each(function(){
- $("#container").append(" router:- " + $(this).attr('id') + "<br />");
- });
Any hint on how to propertly iterate over an XML file using "parseXML"?
Thanks in advance.