Javascript 如何使用 jQuery 解析 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7228141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:23:52  来源:igfitidea点击:

How to parse XML using jQuery?

javascriptjqueryxml

提问by SRA

How do I parse XML, and how can I navigate the result using jQuery? Here is my sample XML:

如何解析 XML,以及如何使用 jQuery 导航结果?这是我的示例 XML:

<Pages>
  <Page Name="test">
    <controls>
      <test>this is a test.</test>
    </controls>
  </Page>
  <Page Name = "User">
    <controls>
      <name>Sunil</name>
    </controls>
  </Page>
</Pages>

I would like to find the node by this path Pages-> Page Name-> controls-> test?

我想通过这个路径找到节点Pages-> Page Name-> controls-> test?

回答by Luwe

There is the $.parseXMLfunction for this: http://api.jquery.com/jQuery.parseXML/

有这个$.parseXML功能:http: //api.jquery.com/jQuery.parseXML/

You can use it like this:

你可以这样使用它:

var xml = $.parseXML(yourfile.xml),
  $xml = $( xml ),
  $test = $xml.find('test');

console.log($test.text());

If you really want an object, you need a plugin for that. This plugin for instance, will convert your XML to JSON: http://www.fyneworks.com/jquery/xml-to-json/

如果你真的想要一个对象,你需要一个插件。例如,此插件会将您的 XML 转换为 JSON:http: //www.fyneworks.com/jquery/xml-to-json/

回答by Rafay

you can use .parseXML

您可以使用 .parseXML

var xml='<Pages>
          <Page Name="test">
           <controls>
              <test>this is a test.</test>
           </controls>  
          </Page>
          <page Name = "User">
           <controls>
             <name>Sunil</name>
           </controls>
          </page>
        </Pages>';

jquery

查询

    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc );
    $($xml).each(function(){
       alert($(this).find("Page[Name]>controls>name").text());
     });

here is the fiddle http://jsfiddle.net/R37mC/1/

这是小提琴http://jsfiddle.net/R37mC/1/

回答by Jens Roland

I assume you are loading the XML from an external file. With $.ajax(), it's quite simple actually:

我假设您是从外部文件加载 XML。使用$.ajax(),实际上很简单:

$.ajax({
    url: 'xmlfile.xml',
    dataType: 'xml',
    success: function(data){
        // Extract relevant data from XML
        var xml_node = $('Pages',data);
        console.log( xml_node.find('Page[Name="test"] > controls > test').text() );
    },
    error: function(data){
        console.log('Error loading XML data');
    }
});

Also, you should be consistent about the XML node naming. You have both lowercase and capitalized node names (<Page>versus <page>) which can be confusing when you try to use XML tree selectors.

此外,您应该对 XML 节点命名保持一致。您有小写和大写的节点名称(<Page>vs <page>),当您尝试使用 XML 树选择器时,这可能会造成混淆。

回答by Vicky

$xml = $( $.parseXML( xml ) );

$xml.find("<<your_xml_tag_name>>").each(function(index,elem){
    // elem = found XML element
});

回答by Felix Kling

Have a look at jQuery's .parseXML()[docs]:

看看 jQuery 的.parseXML()[docs]

var $xml = $(jQuery.parseXML(xml));

var $test = $xml.find('Page[Name="test"] > controls > test');

回答by mbarnettjones

I went the way of jQuery's .parseXML()however found that the XML path syntax of 'Page[Name="test"] > controls > test'wouldn't work (if anyone knows why please shout out!).

我采用了 jQuery 的方式,.parseXML()但是发现 XML 路径语法'Page[Name="test"] > controls > test'不起作用(如果有人知道为什么请大声说出来!)。

Instead I chained together the individual .find()results into something that looked like this:

相反,我将各个.find()结果链接在一起,看起来像这样:

$xmlDoc.find('Page[Name="test"]')
       .find('contols')
       .find('test')

The result achieves the same as what I would expect the one shot find.

结果与我期望的一次性发现相同。

回答by WTK

First thing that pop-up in google results http://think2loud.com/224-reading-xml-with-jquery/There's no simple way to access xml structure (like you described Pages->pagename->controls->test) in jQuery without any plugins.

在 google 结果中弹出的第一件事http://think2loud.com/224-reading-xml-with-jquery/没有简单的方法来访问 xml 结构(就像你描述的Pages->pagename->controls->test)在没有任何插件的 jQuery 中。