JAVA element.getElementsByTagName 限制为顶级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1241525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA element.getElementsByTagName Restrict to Top Level
提问by user152090
I have an XML file as follows:
我有一个 XML 文件,如下所示:
<rootNode>
<link>http://rootlink/</link>
<image>
<link>http://imagelink/</link>
<title>This is the title</title>
</image>
</rootNode>
The XML Java code using DOM is as follows:
使用DOM的XML Java代码如下:
NodeList rootNodeList = element.getElementsByTagName("link");
This will give me all of the "link" elements including the top level and the one inside the "image" node.
这将为我提供所有“链接”元素,包括顶层和“图像”节点内的元素。
Is there a way to just get the "link" tags for rootNode within one level and not two such as is the case for the image link? That is, I just want the http://rootlink/"link".
有没有办法只在一个级别而不是两个级别内获取 rootNode 的“链接”标签,例如图像链接的情况?也就是说,我只想要http://rootlink/“链接”。
回答by Rich Seller
If you can use JDOMinstead, you can do this:
如果您可以改用JDOM,则可以这样做:
element.getChildren("link");
With standard Dom the closest you can get is to iterate the child nodes list (by calling getChildNodes() and checking each item(i) of the NodeList, picking out the nodes with the matching name.
使用标准 Dom,您可以获得的最接近的是迭代子节点列表(通过调用 getChildNodes() 并检查 NodeList 的每个 item(i),挑选具有匹配名称的节点。
回答by McDowell
回答by ZZ Coder
I couldn't find any methods to do that either so I wrote this helper function,
我也找不到任何方法来做到这一点,所以我写了这个辅助函数,
public static List<Element> getChildrenByTagName(Element parent, String name) {
List<Element> nodeList = new ArrayList<Element>();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE &&
name.equals(child.getNodeName())) {
nodeList.add((Element) child);
}
}
return nodeList;
}
回答by Danimate
I wrote this function to get the node value by tagName, restrict to top level
我写了这个函数来通过 tagName 获取节点值,限制到顶级
public static String getValue(Element item, String tagToGet, String parentTagName) {
NodeList n = item.getElementsByTagName(tagToGet);
Node nodeToGet = null;
for (int i = 0; i<n.getLength(); i++) {
if (n.item(i).getParentNode().getNodeName().equalsIgnoreCase(parentTagName)) {
nodeToGet = n.item(i);
}
}
return getElementValue(nodeToGet);
}
public final static String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
回答by ThePerson
I know this is an old question, but nonetheless I have an alternative solution to add.
我知道这是一个老问题,但尽管如此,我还有一个替代解决方案要添加。
It's not the most efficient, but works:
这不是最有效的,但有效:
Get all the children using getElementsByTagName, then just check each one has the same parent to the one you started with.
使用 getElementsByTagName 获取所有子项,然后检查每个子项与您开始使用的子项具有相同的父项。
I use this because I have a set of results, each result can have results nested inside it. When I call my Result constructor I need to add any nested results, but as they themselves will look for their own children I don't want to add children to the current level (their parent will add them).
我使用它是因为我有一组结果,每个结果都可以嵌套在其中的结果。当我调用我的 Result 构造函数时,我需要添加任何嵌套的结果,但由于他们自己会寻找自己的孩子,我不想将孩子添加到当前级别(他们的父母会添加他们)。
Example:
例子:
NodeList children = resultNode.getElementsByTagName("result");
for(int i = 0; i<children.getLength(); i++){
// make sure not to pick up grandchildren.
if(children.item(i).getParentNode().isSameNode(resultNode)){
addChildResult(new Result((Element)children.item(i)));
}
}
Hope this helps.
希望这可以帮助。