简体   繁体   中英

How can I iterate this XML API response in js or jquery

I am getting XML response from one of the API which I have to iterate and extract required data from it. As I am working on XML response for the first time I am having some difficulties to do it.

Below is the XML response file:

<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>

I have to extract following data: 1> d:key 2> d:SR_NAME 3> d:REGION

I am using javascript and jquery. Thanks in advance for helping.

You can use jquery parse xml plugin:

You can get more information here: https://api.jquery.com/jQuery.parseXML/

YOu need to use it like below:

  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() )  
  });

You can also do this using DOM parser like below

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);

DEMO

 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); 

Refer to:

DOM Parser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM