简体   繁体   中英

Getting error in parsing an XML file using JDOM

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).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM