【问题标题】:Code to check particular element is the last child in XML Parser检查特定元素的代码是 XML Parser 中的最后一个子元素
【发布时间】:2019-04-05 11:11:54
【问题描述】:

我在 XML 代码中有以下结构和

<drinks>
<soft>
<subject>
<soft>
<subject>
<brand-name></brand-name>
<subject>
<brand-name></brand-name>
</subject>
</subject>
<link></link>    
<soft>
<nature>
<brand-name></brand-name>
<link></link>
</nature>
<info></info>
</drinks>

我需要确认&lt;info&gt;&lt;drinks&gt; 的最后一个孩子我不知道这样做...我在下面尝试

var $element = $xml.find("drinks").addBack("drinks");
alert($element.children().last().nodeName);

我还需要确认每个&lt;subject&gt; &lt;brand-name&gt; 是第一个孩子。我试试下面的代码

function check_first_child(parent, child, rule, error_type)
{
    if(($xml.find(parent).length > 0))
    {
        var $parent = $xml.find(parent).addBack(parent);
        $parent.children().first().each(function()
        {
            alert(this.nodeName);
        });
    }
}

但是上面的代码只找到了一次主题。我总共有两个主题。

【问题讨论】:

  • @trincot 你能指导我吗

标签: jquery ajax xml-parsing


【解决方案1】:
var $element = $xml.find("drinks").addBack("drinks");
var lastelement = $element.children().last().get(0).nodeName;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多