【问题标题】:jquery parseXML : Properly traverse XML file hierarchicallyjquery parseXML:正确地分层遍历 XML 文件
【发布时间】:2018-08-22 14:44:08
【问题描述】:

我有一个需要使用 jquery "parseXML" 解析的 XML 文件(如下)。

>>> 我无法从与该主题相关的现有已回答问题中找到答案。

目标:

遍历 XML 文件的层次结构并遍历迭代以提取数据。

  • 列出所有“mycase”的数组
    • 对于每个“mycase”迭代,所有“testrun”数组中的列表
      • 对于每个“testrun”迭代,所有“路由器”的数组中的列表
        • 对于每个“路由器”迭代,所有“cmdname”数组中的列表

和“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

问题:

我正在使用下面的代码,但我无法对元素进行分层迭代。标签是全局读取的,而不是每个子元素。

对于每个测试运行,所有路由器都被列出,甚至那些属于其他测试运行的路由器。 对于每个测试运行中的每个路由器,都会列出所有命令,甚至是属于其他路由器的命令

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 />");
                });             
            });
         });
		});  
});
<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>



});

尝试使用以下两种格式来获取元素的特定子元素,但没有成功:
两者都列出了 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 />");
});

关于如何使用“parseXML”正确地迭代 XML 文件的任何提示?

提前致谢。

http://jsfiddle.net/AJNOURI/3pw9cj69/102/

【问题讨论】:

    标签: javascript jquery xml xml-parsing


    【解决方案1】:

    永远不要回到 $xml。始终在当前级别开始您的查找或孩子。我添加了几个变量来清理代码。

    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 />");
            });
          });
        });
      });
    });
    

    http://jsfiddle.net/jakecigar/3pw9cj69/142/

    【讨论】:

    • 感谢@jake-wolpert 的大力帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2014-12-11
    • 2014-03-11
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多