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
null, childElement itself, it's node name or something else?