用jQuery解析复杂的XML [英] Parsing complex XML with jQuery

查看:68
本文介绍了用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">&lt;img src=&#39;[!--$wcmUrl&amp;amp;x28;&#39;resource&amp;#39;,&amp;#39;CMS3_130980&amp;#39;&amp;amp;x29;--]&amp;#39;/&gt;</wcm:element>
    <wcm:element name="image">&lt;img src="[!--$wcmUrl('resource','CMS3_132821')--]"/&gt;</wcm:element>
    <wcm:element name="body">&lt;p&gt;
        Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt;
        Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt;
        Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt;
        Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt;
        Paragraph of text goes here.&lt;/p&gt;
</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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆