Somehow the parsed XML file seems to be wrong or am I doing something wrong?
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root>
<quests>
<quest id="test">
<question>Just a little sentence.</question>
</quest>
</quests>
</root>
I parse it like that:
File file = new File("file.xml");
DocumentBuilderFactory dF = DocumentBuilderFactory.newInstance();
dF.setNamespaceAware(true);
DocumentBuilder dB = dF.newDocumentBuilder();
Document XML = dB.parse(file);
// ------------------------------------------
Element doc = XML.getDocumentElement();
System.out.println(doc.getChildNodes());
Element dx = (Element) doc.getElementsByTagName("quest").item(0);
System.out.println(dx.getAttribute("id"));
System.out.println(dx.getAttribute("id").length());
System.out.println(dx.getAttributes().getLength());
And this is what I get:
[root: null]
0
0
How could that be? How could I parse attributes of elements/tags/nodes?
I just created a new XML file and with that it works!
I think the problem was maybe an hidden character inside the id
which causes the error…