如何使用jQuery解析XML? [英] How to parse XML using jQuery?
本文介绍了如何使用jQuery解析XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何解析XML,以及如何使用jQuery导航结果?这是我的示例XML:
How do I parse XML, and how can I navigate the result using jQuery? Here is my sample XML:
<Pages>
<Page Name="test">
<controls>
<test>this is a test.</test>
</controls>
</Page>
<Page Name = "User">
<controls>
<name>Sunil</name>
</controls>
</Page>
</Pages>
我想通过此路径找到节点 Pages
- > 页面名称
- > 控件
- > test
?
I would like to find the node by this path Pages
-> Page Name
-> controls
-> test
?
推荐答案
这里有 $。parseXML
函数: http://api.jquery.com/jQuery.parseXML/
您可以像这样使用它:
var xml = $.parseXML(yourfile.xml),
$xml = $( xml ),
$test = $xml.find('test');
console.log($test.text());
如果你真的想要一个对象,你需要一个插件。例如,这个插件会将您的XML转换为JSON: http://www.fyneworks。 com / jquery / xml-to-json /
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/
这篇关于如何使用jQuery解析XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文