在JavaScript中以不同的层次结构级别访问重复的XML标签 [英] Accessing repeated XML tags at differing hierarchy levels in JavaScript

查看:104
本文介绍了在JavaScript中以不同的层次结构级别访问重复的XML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下层次结构的XML文件;

I have an XML file which uses the following hierarchy structure;

    item
        item
            (item details)
        item
            (item details)
    item
        item
            (item details)
        item
            (item details)
        item
            (item details)

等.

我希望能够在第一级访问每个"item"标签,对于每个标签,在该级别(第二级)访问"item"并将其详细信息(第三级)分配给单个对象对于每个第二级项目.我可以使用jQuery搜索每个item标签,但是显然这在两个级别都通过了'item'标签,而不仅仅是在第一级.

I want to be able to access each of the 'item' tags at the first level, and for each of those, access the 'item's within that (second level) and assign its details (third levels) to an individual object for each second level item. I can search using jQuery for each item tag, however this obviously goes through 'item' tags at both levels, rather than just the first level.

如何检索每个第一级项目,然后将其用作访问其中的项目的参考(因此每个第二级项目都一次被处理)?

How can I retrieve each first level item, and then use that as a reference to access the items within it (so each of the second level items are processed one at a time)?

试图尽可能清楚地说明这一点,我不喜欢重复使用"item"标签,但可悲的是它超出了我的控制范围..谢谢.

Tried to word this as clearly as possible, I don't like the reuse of the "item" tag but it's out of my control sadly.. Thanks.

推荐答案

我建议反过来做:

  • $("item>item")将仅使您可以访问第二级项目标签.
  • 对于每个第二级项目,使用parent()访问相关的第一级项目
  • $("item>item") will give you access to the second level item tags only.
  • for each second level item, use parent() to access the related first level item

您还可以使用$("item")获取所有项目,然后在循环中使用$(this).find("item")获取第二级项目.如果$(this).find("item")的长​​度为0,则表示this是第二行项目:

You could also use $("item") to get all items, then use $(this).find("item") in a loop to get the second level items. If $(this).find("item") has a length of 0, it means that this is a second row item:

$("item").each(function(){
    var childrenItems=$(this).find("item");
    if (childrenItems.length) {
        // process the children items
    }
});

这篇关于在JavaScript中以不同的层次结构级别访问重复的XML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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