gpt4 book ai didi

java - JDOM.Element.getChild(String) 返回意外结果

转载 作者:行者123 更新时间:2023-11-29 08:16:27 24 4
gpt4 key购买 nike

根据 jdom.org 处的 API , getChild(String name) 的语义:

This returns the first child element within this element with the given local name and belonging to no namespace. If no elements exist for the specified name and namespace, null is returned.

因此,如果我有一个像这样的 XML 结构:

<?xml version="1.0" encoding="UTF-8"?>
<lvl1>
<lvl2>
<lvl3/>
</lvl2>
</lvl1>

我有一个当前指向 <lvl1> 的 JDOM 元素.我应该能够进行以下调用:

Element lvl3 = lvl1Element.getChild("lvl3");

lvl3应该有非空值。

但是,我发现 lvl3实际上是null .我错过了什么吗?

这是一个应该有效的示例代码片段:

import java.io.StringReader;
import org.jdom.*;
public static void main(String[] args){
Document doc = new SAXBuilder().build(new StringReader("path to file"));
Element lvl1Element = doc.getRootElement();
Element lvl3Element = lvl1Element.getChild("lvl3"); //is null. Why?
}

最佳答案

为了获得我正在寻找的功能,我使用了来自 jdom.orggetDescendants(ElementFilter) 函数的 Iterator

然后我通过使用类似于以下的代码得到了我正在寻找的 Element:

Element lvl3 = lvl1.getDescendants(new ElementFilter("lvl3"));

关于java - JDOM.Element.getChild(String) 返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4493537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com