使用 JDOM 解析 XML 文件时出错

发布于 2024-11-19 23:03:30 字数 723 浏览 3 评论 0原文

我有这个 XML 文档:

<?xml version="1.0" encoding="utf-8"?>
<RootElement>
   <Achild>
      .....
   </Achild>
</RootElement>

如何检查文档是否包含 Achild 元素?我尝试过

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
try {
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.parse(configFile);
    final Node parentNode = doc.getDocumentElement();
    final Element childElement = (Element) parentNode.getFirstChild();
    if(childElement.getNodeName().equalsIgnoreCase(...

,但它给了我一个错误(childElement 为空)。

I have this XML document:

<?xml version="1.0" encoding="utf-8"?>
<RootElement>
   <Achild>
      .....
   </Achild>
</RootElement>

How can I check if the document contains Achild element or not? I tried

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
try {
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.parse(configFile);
    final Node parentNode = doc.getDocumentElement();
    final Element childElement = (Element) parentNode.getFirstChild();
    if(childElement.getNodeName().equalsIgnoreCase(...

but it gives me an error (childElement is null).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-11-26 23:03:30

我认为您将 #text 节点(位于 之间)作为第一个子节点(这很漂亮)常见错误),例如:

final Node parentNode = doc.getDocumentElement();
Node childElement = parentNode.getFirstChild();
System.out.println(childElement.getNodeName());

返回:

#text

使用替代:

final Node parentNode = doc.getDocumentElement();
NodeList childElements = parentNode.getChildNodes();
for (int i = 0; i < childElements.getLength(); ++i)
{
    Node childElement = childElements.item(i);
    if (childElement instanceof Element)
        System.out.println(childElement.getNodeName());
}

想要的结果:

Achild

编辑:

还有第二种方法使用 DocumentBuilderFactory.setIgnoringElementContentWhitespace< /code>方法:

factory.setIgnoringElementContentWhitespace(true);

但是,这只适用于验证模式,因此您需要在 XML 文档中提供 DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE RootElement [
    <!ELEMENT RootElement (Achild)+>
    <!ELEMENT Achild (#PCDATA)>
]>
<RootElement>
   <Achild>some text</Achild>
</RootElement>

并设置factory.setValidating(true)。完整示例:

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse("input.xml");

final Node rootNode = doc.getDocumentElement();
final Element childElement = (Element) rootNode.getFirstChild();

System.out.println(childElement.getNodeName());

原始代码想要的结果:

Achild

I think that you're getting #text node (that between <RootElement> and <Achild>) as first child (that's pretty common mistake), for example:

final Node parentNode = doc.getDocumentElement();
Node childElement = parentNode.getFirstChild();
System.out.println(childElement.getNodeName());

Returns:

#text

Use instead:

final Node parentNode = doc.getDocumentElement();
NodeList childElements = parentNode.getChildNodes();
for (int i = 0; i < childElements.getLength(); ++i)
{
    Node childElement = childElements.item(i);
    if (childElement instanceof Element)
        System.out.println(childElement.getNodeName());
}

Wanted result:

Achild

EDIT:

There is second way using DocumentBuilderFactory.setIgnoringElementContentWhitespace method:

factory.setIgnoringElementContentWhitespace(true);

However this works only in validating mode, so you need to provide DTD in your XML document:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE RootElement [
    <!ELEMENT RootElement (Achild)+>
    <!ELEMENT Achild (#PCDATA)>
]>
<RootElement>
   <Achild>some text</Achild>
</RootElement>

and set factory.setValidating(true). Full example:

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse("input.xml");

final Node rootNode = doc.getDocumentElement();
final Element childElement = (Element) rootNode.getFirstChild();

System.out.println(childElement.getNodeName());

Wanted result with original code:

Achild
七度光 2024-11-26 23:03:30

听起来 .getFirstChild() 返回一个包含“”和“”之间空格的文本节点,在这种情况下,您需要前进到下一个同级节点才能到达您期望的位置。

It sounds like .getFirstChild() is returning you a text node containing the white space between "" and "", in which case you would need to advance to the next sibling node to get to where you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文