【问题标题】:jQuery Outputting Duplicate XML Child Nodes IncorrectlyjQuery错误地输出重复的XML子节点
【发布时间】:2014-02-10 14:56:28
【问题描述】:

我正在使用 jQuery 来解析和输出 XML。我有一个嵌套循环来输出 TASKS 的所有子代,但每次迭代都会立即输出所有子代,而不是每行一个。

<xml version="1.0">
<choices>
    <choice id="1" name="Type 1">
        <description>
            Example a
        </description>
        <tasks>
            <task>Do this</task>
            <task>Do that</task>
        </tasks>
    </choice>
    <choice id="2" name="Type 2">
        <description>
            Example b
        </description>
        <tasks>
            <task>Do other</task>
            <task>Do something</task>
        </tasks>
    </choice>
</choices>

我的脚本:

$(function(){
        "use strict";
//Above XML content is being passed as a string for this example
        var str = "<xml version='1.0'><choices><choice id=\"1\" name=\"Type 1\"><description>Example a</description><tasks><task>Do this</task><task>Do that</task></tasks></choice><choice id=\"2\" name=\"Type 2\"><description>Example b</description><tasks><task>Do other</task><task>Do something</task></tasks></choice></choices></xml>";
        var xmlDoc = $.parseXML(str);
        var $xml = $(xmlDoc);

        var temp = {};

        $xml.find('choice').each(function() {
            var $this = $(this);
            temp.id = $this.attr('id');
            temp.title = $this.attr('name');
            temp.description = $this.find('description').text();
            temp.choices = [];

            var counter = 0;

            $this.find('tasks').eq( counter ).each(function(index, child) {

                document.write( $(child).find("task").text() + counter + "<br>");
                counter++;
            });


        });

    });

我想看到的任务子级的输出:

Do this0
Do that1
Do other2
Do something3

我目前看到的输出:

Do thisDo that0
Do otherDo something0

【问题讨论】:

    标签: jquery xml xml-parsing


    【解决方案1】:

    如果您只需要打印任务,这可以大大简化为:

    $xml.find('task').each(function (i) {
        document.write($(this).text() + i + "<br>");
    });
    

    JSFiddle


    如果您需要 choice 循环来执行您刚刚在此问题中遗漏的其他事情,您可以将您的任务循环更改为:

            $this.find('task').each(function() {
    
                document.write( $(this).text() + counter + "<br>");
                counter++;
            });
    

    并将counter 声明移到choice 循环上方

    JSFiddle
    (检查jsfiddle中的控制台,在您的实际代码中将console.log改回document.write

    上面将循环遍历当前choice标签内的所有task标签。

    如果您只想查看tasks 的子代,而不管标签。你可以这样做:

    $this.find('tasks').children().each(function() { ...
    

    如果您想确保只找到 task 的子 tasks,您可以这样做:

    $this.find('tasks').children("task").each(function() {
    

    上述所有 3 项都将与您当前的 xml 产生相同的结果(这是您的预期结果)。


    问题是您在each() 循环中查找“任务”而不是每个单独的“任务”,并从其中的所有task 标记中获取文本。相反,循环遍历每个单独的任务。

    进一步分解您的原始代码在做什么......

    var counter = 0;
    //find the first `tasks` element within `$this`, then loop through it (the single item first task)
    $this.find('tasks').eq( counter ).each(function(index, child) {
                //find all the text within all the `task` tags. put 0 and a <br> after
                document.write( $(child).find("task").text() + counter + "<br>");
                //increment counter, doesn't matter since this block of code will only be ran once
                counter++;
            });
    

    【讨论】:

    • 出色之处在于您的回答简单明了。我显然是 Rube Goldberg 的粉丝。
    • 另外,如果这就是你需要做的一切.. 有很多不必要的东西。您可以摆脱choice 循环,只需执行$xml.find('task').each(function(i) { document.write($(this).text()+i+"&lt;br&gt;")});,请参见此处:jsfiddle.net/fhLSd/1
    • 我正在做的事情有点复杂,但是我输出了我所做的每个逻辑步骤,因为这最终将成为数组中对象中的数组。我很欣赏所有可能的输出方式。
    猜你喜欢
    • 2019-01-18
    • 2023-03-15
    • 2011-07-30
    • 2015-03-13
    • 2020-05-26
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多