I got some trouble parsing an XML document. For some reason, there are text nodes where I would not expect them to be and therefore my test turns red. The XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
<PR1>PR1</PR1>
<ROL>one</ROL>
<ROL>two</ROL>
<DG1>DG1</DG1>
<ROL>three</ROL>
<ZBK>ZBK</ZBK>
<ROL>four</ROL>
</RootNode>
Now I have this snippet of code which can reproduce the error:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(TestHL7Helper.class.getResourceAsStream("TestHL7HelperInput.xml"));
Node root = doc.getFirstChild();
Node pr1 = root.getFirstChild();
Inspecting the root variable yields [RootNode: null]
which seems to be right, but then it somehow goes all wrong. The pr1
variable turns out to be a text node [#text:\n ]
- but why does the parser think that the new line and the spaces are a text node? Shouldn't that be ignored? I tried changing the encoding but that did not help either. Any ideas on that?
If I remove all new lines and space and have my XML document in just one line it all works fine...