我有一个XML文件(下面)我需要使用jquery" parseXML "进行解析。
>>>我无法从与该主题相关的现有答案问题中找到答案。<<<
遍历XML文件的层次结构并循环遍历迭代以提取数据。
和" cmdfile"
最佳结果是每次都有每个级别的元素数量,然后循环遍历迭代次数以显示内容(例如:到页面)并继续在更深层次上进行相同操作等等...
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
我正在使用下面的代码,我无法对元素进行分层迭代。全局读取标记而不是每个子元素。
对于每个testrun,列出所有路由器,甚至是属于其他测试的路由器。 对于每个testrun中的每个路由器,列出所有命令,甚至是属于其他路由器的命令
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>
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 />");
});
});
});
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<body>
<div id="container"/>
</body>
</html>
});
&#13;
尝试使用以下两种格式来获取元素的特定子元素,但没有成功:
两者都列出了XML文档中的所有路由器。
$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 />");
});
有关如何使用&#34; parseXML&#34;对
进行资产迭代的任何提示?提前致谢。
答案 0 :(得分:1)
永远不要回到$ xml。始终以当前级别开始您的发现或儿童。我添加了几个vars来清理代码。
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() {
var mycase = $(this);
$("#container").append(" Casename:- " + mycase.find("casename").text() + "<br />");
$("#container").append(" topofile:- " + mycase.find("topo").text() + "<br />");
$("#container").append(" Case id:- " + mycase.attr("id") + "<br />");
mycase.find("testrun").each(function() {
var testrun = $(this);
$("#container").append("--- testrun id:- " + testrun.attr("id") + "<br />");
testrun.children("router").each(function() {
var router = $(this);
$("#container").append("------ router id:- " + router.attr("id") + "<br />");
router.find("command").each(function() {
var command = $(this);
$("#container").append("--------- cmdname:- " + command.find("cmdname").text() + ", cmdfile:- " + command.find("cmdfile").text() + "<br />");
});
});
});
});
});