用jQuery解析复杂的XML [英] Parsing complex XML with jQuery
本文介绍了用jQuery解析复杂的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下jQuery解析以下XML.页面上什么都没有显示.我知道它正在获取文件.我已经看过网上了,尽管有很多使用jquery解析xml的示例,但没有一个看起来像这种格式.
I'm trying to parse the following XML with the following jQuery. Nothing is displayed on the page. I know it's getting the file. I've looked online and though there are many examples of parsing xml with jquery none of them looked like this formatting.
XML:
<?xml version="1.0" encoding="utf-8"?>
<wcm:root xmlns:wcm="http://www.stellant.com/wcm-data/ns/8.0.0" version="8.0.0.0">
<wcm:element name="title"></wcm:element>
<wcm:element name="wide_image"><img src='[!--$wcmUrl&amp;x28;'resource&#39;,&#39;CMS3_130980&#39;&amp;x29;--]&#39;/></wcm:element>
<wcm:element name="image"><img src="[!--$wcmUrl('resource','CMS3_132821')--]"/></wcm:element>
<wcm:element name="body"><p>
Paragraph of text goes here.<br /><br />
Paragraph of text goes here.<br /><br />
Paragraph of text goes here.<br /><br />
Paragraph of text goes here.<br /><br />
Paragraph of text goes here.</p>
</wcm:element>
</wcm:root>
HTML& jQuery:
HTML & jQuery:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "link_to_my_file.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml){
$(xml).find("wcm\\3a root").each(function(){
alert("Test");
$("#output").append($(this).attr("title") + "<br />");
$("#output").append($(this).attr("wide_image") + "<br />");
$("#output").append($(this).attr("image") + "<br />");
$("#output").append($(this).attr("body") + "<br />");
});
};
</script>
<div id="output"></div>
</html>
推荐答案
您必须转义选择器中的:
: http://mothereff.in/css-escapes#1wcm%3Aroot
所以,而不是:
$(xml).find("wcm:root")
使用:
$(xml).find("wcm\\3a root")
此外,您的代码段是无效的JavaScript(在parseXml
函数声明之后缺少}
).检查您的错误控制台.
Also, your code snippet is invalid JavaScript (missing }
after the parseXml
function declaration). Check your error console.
这篇关于用jQuery解析复杂的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文