如何在js或jquery中迭代此XML API响应

时间:2018-01-04 04:35:26

标签: javascript jquery xml

我从其中一个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 GT; d:key 2 - ; d:SR_NAME 3 GT; d:REGION

我正在使用javascript和jquery。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用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() )  
  });

答案 1 :(得分:0)

您也可以使用下面的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