【问题标题】:How can I iterate this XML API response in js or jquery如何在 js 或 jquery 中迭代此 XML API 响应
【发布时间】:2018-01-04 04:35:26
【问题描述】:

我从其中一个 API 获取 XML 响应,我必须对其进行迭代并从中提取所需数据。当我第一次处理 XML 响应时,我遇到了一些困难。

以下是 XML 响应文件:

<feed xmlns:d="http://schemas.example.com/a" xmlns:m="http://schemas.example.com/a" xmlns="http://www.w3.org/2005/Atom" xml:base="http://demo_url:3000/url/example.xsodata/">
    <title type="text">EMC_SR_NUMB</title>
    <id>
    http://demo_url:3000/url/example.xsodata/EMC_SR_NUMB
    </id>
    <author>
    <name/>
    </author>
    <link rel="self" title="EMC_SR_NUMB" href="EMC_SR_NUMB"/>
    <entry>
        <id>
        http://demo_url:3000/url/example.xsodata/EMC_SR_NUMB('204713171')
        </id>
        <title type="text"/>
        <author>
        <name/>
        </author>
        <link rel="self" title="EMC_SR_NUMB" href="EMC_SR_NUMB('204713171')"/>
        <category term="infra.infrae.EMC_SR_NUMBType" scheme="http://schemas.microsoft.com/a"/>
        <content type="application/xml">
            <m:properties>
                <d:key m:type="Abc.String">204713171</d:key>
                <d:SR_NAME m:type="Abc.String">CLOV - 16RT</d:SR_NAME>
                <d:REGION m:type="Abc.String">GENERAL</d:REGION>
            </m:properties>
        </content>
    </entry>
    <entry>
        <id>
        http://demo_url:3000/url/example.xsodata/EMC_SR_NUMB('204713172')
        </id>
        <title type="text"/>
        <author>
        <name/>
        </author>
        <link rel="self" title="EMC_SR_NUMB" href="EMC_SR_NUMB('204713172')"/>
        <category term="infra.infrae.EMC_SR_NUMBType" scheme="http://schemas.microsoft.com/a"/>
        <content type="application/xml">
            <m:properties>
                <d:key m:type="Abc.String">204713172</d:key>
                <d:SR_NAME m:type="Abc.String">CLOV - 16RT</d:SR_NAME>
                <d:REGION m:type="Abc.String">TRENDS</d:REGION>
            </m:properties>
        </content>
    </entry>
</feed>

我必须提取以下数据: 1>d:key 2>d:SR_NAME 3>d:REGION

我正在使用 javascript 和 jquery。提前感谢您的帮助。

【问题讨论】:

    标签: javascript jquery xml


    【解决方案1】:

    你可以使用jquery parse xml插件:

    您可以在此处获取更多信息: https://api.jquery.com/jQuery.parseXML/

    你需要像下面这样使用它:

      var xml = res, // YOur xml response will be accepted here.
      xmlDoc = $.parseXML( xml ),
    
    
    
     $xmlDoc .find("entry").each(function(){
         console.log( $(this).find("content").find("m\\:properties").find("content\\:SR_NAME").text() ) ;
         //console.log( $(this).find("content\\:SR_NAME").text() )
        // console.log( $(this).find("content\\:REGION").text() )  
      });
    

    【讨论】:

    • 谢谢,@Pra Jazz,我得到了文本,但是当我写 $.parseXML(xml);我什么也得不到。再加上一件事。控制台输出是“204713171CLOV - 16RTTRENDS”我需要单独使用它。
    • 你能添加这个的jsfiddle吗
    • 那我也许可以玩转它
    • 真的不知道怎么加JS fiddle。
    • 控制台输出是“204713171CLOV - 16RTTRENDS”我需要单独使用它。
    【解决方案2】:

    您也可以使用DOM parser 来执行此操作,如下所示

    let parser = new DOMParser();
    let doc = parser.parseFromString(s, "application/xml");
    
    let mprops = doc.getElementsByTagName('m\:properties');
    let res=[];
    for (let i = 0; i < mprops.length; i++) {
        let o={};
        o.dkey = mprops[i].getElementsByTagName('d\:key')[0].innerHTML;
        o.dSRNAME = mprops[i].getElementsByTagName('d\:SR_NAME')[0].innerHTML;
        o.dREGION = mprops[i].getElementsByTagName('d\:REGION')[0].innerHTML;
        res.push(o);
    }
    console.log(res);
    

    演示

    let s = `<feed xmlns:d="http://schemas.example.com/a" xmlns:m="http://schemas.example.com/a" xmlns="http://www.w3.org/2005/Atom" xml:base="http://demo_url:3000/url/example.xsodata/">
    <title type="text">EMC_SR_NUMB</title>
    <id>
    http://demo_url:3000/url/example.xsodata/EMC_SR_NUMB
    </id>
    <author>
    <name/>
    </author>
    <link rel="self" title="EMC_SR_NUMB" href="EMC_SR_NUMB"/>
    <entry>
        <id>
        http://demo_url:3000/url/example.xsodata/EMC_SR_NUMB('204713171')
        </id>
        <title type="text"/>
        <author>
        <name/>
        </author>
        <link rel="self" title="EMC_SR_NUMB" href="EMC_SR_NUMB('204713171')"/>
        <category term="infra.infrae.EMC_SR_NUMBType" scheme="http://schemas.microsoft.com/a"/>
        <content type="application/xml">
            <m:properties>
                <d:key m:type="Abc.String">204713171</d:key>
                <d:SR_NAME m:type="Abc.String">CLOV - 16RT</d:SR_NAME>
                <d:REGION m:type="Abc.String">GENERAL</d:REGION>
            </m:properties>
        </content>
    </entry>
    <entry>
        <id>
        http://demo_url:3000/url/example.xsodata/EMC_SR_NUMB('204713172')
        </id>
        <title type="text"/>
        <author>
        <name/>
        </author>
        <link rel="self" title="EMC_SR_NUMB" href="EMC_SR_NUMB('204713172')"/>
        <category term="infra.infrae.EMC_SR_NUMBType" scheme="http://schemas.microsoft.com/a"/>
        <content type="application/xml">
            <m:properties>
                <d:key m:type="Abc.String">204713172</d:key>
                <d:SR_NAME m:type="Abc.String">CLOV - 16RT</d:SR_NAME>
                <d:REGION m:type="Abc.String">TRENDS</d:REGION>
            </m:properties>
        </content>
    </entry>
    </feed>`;
    
    let parser = new DOMParser();
    let doc = parser.parseFromString(s, "application/xml");
    
    let mprops = doc.getElementsByTagName('m\:properties');
    let res=[];
    for (let i = 0; i < mprops.length; i++) {
        let o={};
        o.dkey = mprops[i].getElementsByTagName('d\:key')[0].innerHTML;
        o.dSRNAME = mprops[i].getElementsByTagName('d\:SR_NAME')[0].innerHTML;
        o.dREGION = mprops[i].getElementsByTagName('d\:REGION')[0].innerHTML;
        res.push(o);
    }
    console.log(res);

    参考:

    DOM Parser

    【讨论】:

    • 我的 mprops.length 为零。我正在使用与您相同的代码。
    • 我得到了我在代码中提到的相同架构
    • 您正在尝试什么代码,请在问题中添加。这可能会有所帮助。
    • 当我使用您的代码时,我收到“第 1 列第 1 行错误:文档为空”
    • 您的问题中没有代码来展示您正在尝试的内容,而是一个 XML 模式。请添加一个最小且可验证的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多