【问题标题】:Get XML item by ID when use xmlhttp-request使用 xmlhttp-request 时按 ID 获取 XML 项
【发布时间】:2013-12-24 15:50:41
【问题描述】:

我开始使用 XML 和 XMLHTTP-Request,但我不明白如何通过 ID 获取项目。例如:

在我的 XML 文档中,我得到了:

<note id="501">
    <to>Exemplo 2</to>
</note>
<note id="502">
    <to>Exemplo 2</to>
</note>

在我的 HTML 中,我得到了:

document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

使用该代码,我只得到第一个 (501),但是如何通过 ID 定义并获取第二个 (502)?

PS.:我从这里得到了例子:http://www.w3schools.com/xml/xml_attributes.asp

【问题讨论】:

    标签: javascript xml xmlhttprequest


    【解决方案1】:

    通过原始 javascript 遍历 XML,你会变得疯狂。获取 jQuery 然后:

    var $xml = $( $.parseXML(xmlDoc) );
    var a = $xml.find('note[id=501]').text();
    var b = $xml.find('note[id=502]').text();
    

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 1970-01-01
      • 2020-08-14
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      相关资源
      最近更新 更多