1

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

4
  • What is null, childElement itself, it's node name or something else? Commented Jul 11, 2011 at 13:03
  • Do you mean a tag called "AChild" or do you mean a child node in general? Commented Jul 11, 2011 at 13:05
  • I mean a child node with name "Achild" Commented Jul 11, 2011 at 13:07
  • is giving something like [#text....] Commented Jul 11, 2011 at 13:08

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

@Saurabh: You're welcome. Note that JDOM API (jdom.org/docs/apidocs) is a different thing from standard Java DOM API (this included in JDK).
I have a question like on doing parentNode.getChildNodes(); will get me sub childnodes of ACHild also or just childs of RootElement.Because I don't want subchilds of Achild and if he is iterating over that then it will cost me performance wise....:)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.