微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JQuery的.递归地解析XML子项.怎么样?

我一直试图找到一个关于如何解析以下 XML文档的示例.下面的示例显示了我正在查看的深度.

我想我需要以下内容

1.加载XML的函数.

$.get('getProfile.xml',null,function (data) {
    // ...
},'xml');

2.循环通过根节点查找子节点的方法.对于找到的每个子节点,遍历找到的子节点的子节点,寻找子节点的新子节点.如果没有找到,只需输出此子节点内的内容即可.

<?xml version="1.0" encoding="utf-8"?>
<GetProfile>
    <UserInfo>
        <id>free2rhyme</id>
        <name>jerry mcguire</name>
        <age>29</age>
        <sex>m</sex>
        <location>salt lake city,utah</location>
        <signup>00/00/0000</signup>
    </UserInfo>
    <Entry>
        <id>13579</id>
        <date>2011-01-24</date>
        <time>9:34:21</time>
        <title>my first jounal entry</title>
        <body>&lt;![CDATA[i'm really excited to have signed up for myjournal.co.cc! Yes!!!]]&gt;</body>
        <Comments>
            <Comment>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <from>otheruser84</from>
                <body>I am glad you really liked the site! Have fun!!</body>
            </Comment>
            <Comment>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <from>otheruser84</from>
                <body>I am glad you really liked the site! Have fun!!</body>
            </Comment>
            <Comment>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <from>otheruser84</from>
                <body>I am glad you really liked the site! Have fun!!</body>
            </Comment>
        </Comments>
    </Entry>
    <Stats>
        <following>40</following>
        <followers>57</followers>
        <entries>158</entries>
        <favorites>15</favorites>
    </Stats>
    <Preview>
        <Entries>
            <Entry>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <title>otheruser84</title>
            </Entry>
            <Entry>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <title>otheruser84</title>
            </Entry>
            <Entry>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <title>otheruser84</title>
            </Entry>
            <Entry>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <title>otheruser84</title>
            </Entry>
            <Entry>
                <id>97531</id>
                <date>2011-02-16</date>
                <time>9:34:21</time>
                <title>otheruser84</title>
            </Entry>
        </Entries>
    </Preview>
</GetProfile>

上面的样本有希望澄清我想要做的事情.

4.这是一种返回节点索引,名称和值的方法,但它不会检查可能也有自己子节点的子节点.

var getProfile = $(data).find('GetProfile').each(function () {

    $('*',this).each(function (index,event) {
        alert('index=' + index + ' name=' + e.tagName + ' value=' + $(e).text());
    });

});

解决方法

This page提供了一个递归遍历XML DOM的片段:

function traverse(tree) {
    if (tree.hasChildNodes()) {
        document.write('<ul><li>');
        document.write('<b>' + tree.tagName + ' : </b>');
        var nodes = tree.childNodes.length;
        for (var i = 0; i < tree.childNodes.length; i++)
        traverse(tree.childNodes(i));
        document.write('</li></ul>');
    } else document.write(tree.text);
}

另一种方法是使用DOM解析器(例如window.DOMParser或ActiveXObject(“Microsoft.XMLDOM”))来解析XML字符串:

if (window.DOMParser) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text,"text/xml");
} 
else { // Internet Explorer
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(text);
}

见本页:http://www.w3schools.com/dom/dom_parser.asp.

编辑:

function traverse(tree) {
    $(tree).contents().each(function() {
        if (this.nodeType == 3) { // text node
            // node value: $(this).text() or this.nodeValue
            console.log('text node value: '+ $(this).text());

        } else {
            console.log('tag name: ' + this.nodeName);
            traverse(this);
        }
    });
}

请参阅此操作:http://jsfiddle.net/william/uKGHJ/1/.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐