简体   繁体   English

使用Jquery和Javascript解析xml

[英]Parse xml with Jquery and Javascript

I would like to parse the following xml flow : 我想解析以下xml流程:

<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. with $(xmlDoc)是文档节点。

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. 它工作正常,但我不知道如何正确解析此文件,以获得包含2个腿块信息的数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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