通过名称获取XML只有直接的子元素 [英] Get XML only immediate children elements by name

查看:98
本文介绍了通过名称获取XML只有直接的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:如果有其他元素与父元素的孙子同名,那么我如何直接在特定的父元素下获取元素。

My question is: How can I get elements directly under a specific parent element when there are other elements with the same name as a "grandchild" of the parent element.

我正在使用Java DOM库解析XML 元素我遇到麻烦。我使用的是xml的一些(一小部分):

I'm using the Java DOM library to parse XML Elements and I'm running into trouble. Here's some (a small portion) of the xml I'm using:

<notifications>
  <notification>
    <groups>
      <group name="zip-group.zip" zip="true">
        <file location="C:\valid\directory\" />
        <file location="C:\another\valid\file.doc" />
        <file location="C:\valid\file\here.txt" />
      </group>
    </groups>
    <file location="C:\valid\file.txt" />
    <file location="C:\valid\file.xml" />
    <file location="C:\valid\file.doc" />
  </notification>
</notifications>

如你所见,有两个地方可以放置 file> 元素。可以是团体或外部团体。现在,每当我调用 notificationElement.getElementsByTagName(file);

As you can see, there are two places you can place the <file> element. Either in groups or outside groups. I really want it structured this way because it's more user-friendly.

/ code>它给我所有的< file> 元素,包括< group> 元素。我处理这些不同的文件,所以这个功能是不可取的。

Now, whenever I call notificationElement.getElementsByTagName("file"); it gives me all the <file> elements, including those under the <group> element. I handle each of these kinds of files differently, so this functionality is not desirable.

我已经想到了两种解决方案:

I've thought of two solutions:


  1. 获取文件元素的父元素并相应处理(取决于它是否为< notification> < group>

  2. 重命名第二个< file> 元素以避免混淆。

  1. Get the parent element of the file element and deal with it accordingly (depending on whether it's <notification> or <group>.
  2. Rename the second <file> element to avoid confusion.

这两个解决方案都不像只是把它们放在一边,只得到 < file> 元素,它们是< notification> 元素的直接子元素。

Neither of those solutions are as desirable as just leaving things the way they are and getting only the <file> elements which are direct children of <notification> elements.

我可以通过 IMPO 对最佳方式的评论和答案开放,但我对DOM 解决方案真的很感兴趣,因为这是其余的项目正在使用。谢谢。

I'm open to IMPO comments and answers about the "best" way to do this, but I'm really interested in DOM solutions because that's what the rest of this project is using. Thanks.

推荐答案

嗯,这个问题的DOM解决方案实际上是tty简单,即使不太优雅当我重复通过 filesNodeList ,当我调用 notificationElement.getElementsByTagName(file); 我只是检查父节点的名称是否是通知。如果不是,那么我忽略它,因为它将由< group> 元素。这是我的代码解决方案:

Well, the DOM solution to this question is actually pretty simple, even if it's not too elegant When I iterate through the filesNodeList which is returned when I call notificationElement.getElementsByTagName("file"); I just check whether the parent node's name is "notification." If it isn't then I ignore it because that will be handled by the <group> element. Here's my code solution:

for (int j = 0; j < filesNodeList.getLength(); j++) {
  Element fileElement = (Element) filesNodeList.item(j);
  if (!fileElement.getParentNode().getNodeName().equals("notification")) {
    continue;
  }
  ...
}

这篇关于通过名称获取XML只有直接的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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