最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery get all node names (both parent and children) - Stack Overflow

programmeradmin1浏览0评论

I have an XML document that gets generated by an object. I have no idea what it will really look like at the time of the jQuery AJAX call. What I would like to do is parse through the XML by parent and child getting the node names.

Any direction you can offer will be of great service.

Thanks!

I have an XML document that gets generated by an object. I have no idea what it will really look like at the time of the jQuery AJAX call. What I would like to do is parse through the XML by parent and child getting the node names.

Any direction you can offer will be of great service.

Thanks!

Share Improve this question edited Jun 17, 2011 at 19:36 webdad3 asked Jun 17, 2011 at 18:28 webdad3webdad3 9,13031 gold badges128 silver badges229 bronze badges 2
  • Do you have control over this webservice? If you do, would you consider returning a JSON object instead of XML? JSON is much easier and faster to work with. – pixelfreak Commented Jun 17, 2011 at 18:33
  • unfortunately I don't have control... It is XML and will stay XML at least until the next version... – webdad3 Commented Jun 17, 2011 at 18:34
Add a ment  | 

2 Answers 2

Reset to default 6 +50

You should be able to parse through XML the same way you parse through DOM elements in jquery;

http://jsfiddle/TBwm8/3/

var xml = "<root><stuff></stuff><stuff><stuffchild></stuffchild></stuff></root>";

function alertit(jqueryObject) {
    if (jqueryObject.length === 0) return;

    jqueryObject.each(function() {
        alert(this.nodeName.toLowerCase());
    });

    alertit(jqueryObject.children());
}

alertit($(xml));

Have you looked at jParse?

发布评论

评论列表(0)

  1. 暂无评论