简体   繁体   中英

Parse xml with Jquery and Javascript

I would like to parse the following xml flow :

<telhdl:leg>
  <tel:deviceId>82085625</tel:deviceId>
  <tel:media>AUDIO</tel:media>
  <tel:state>ACTIVE</tel:state>
  <tel:capabilities>
    <tel:drop>true</tel:drop>
    <tel:hold>true</tel:hold>
    <tel:mute>true</tel:mute>
    <tel:sendDtmf>true</tel:sendDtmf>
  </tel:capabilities>
</telhdl:leg>
<telhdl:leg>
  <tel:deviceId>82085625</tel:deviceId>
  <tel:media>VIDEO</tel:media>
  <tel:state>ACTIVE</tel:state>
  <tel:muted>true</tel:muted>-
  <tel:capabilities>
    <tel:drop>true</tel:drop>
    <tel:unMute>true</tel:unMute>
  </tel:capabilities>
</telhdl:leg>

As you can see, there is 2 groups of leg, but in one of them there is an attributes which is not present in the other (muted) for example.

I have tried to parse it using this code :

$(xmlDoc).find('telhdl\\\\\\\\:deviceId,deviceId'); with $(xmlDoc) is the document node.

It works fine, but i don't know how to parse correctly this file to have as result an array which contain information of the 2 legs block.

The question is more : How to have clerary a result of the parsing ?

This should do the trick:

$(xmlDoc).find("telhdl").each(function() {
    var deviceId = $(this).find("deviceId").text();
    var media = $(this).find("media").text();
    ....etc....
    var array = new Array(deviceId,media,...etc...);
});

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