I'm parsing an XML document with VTD-XML library and need to get version tag from the document.
My document looks like this;
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<rootNode>
<items>
<item>
<name>XXX</name>
<lastName>YYY</lastName>
<number>1234</number>
</item>
<item>
<name>AAA</name>
<lastName>BBB</lastName>
<number>5678</number>
</item>
<item>
<name>CCC</name>
<lastName>DDD</lastName>
<number>9012</number>
</item>
</items>
</rootNode>
I need to get this line.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
How can I do it?
I don't know how to do it in VTD-XML. But the following answer explains how to do it in DOM. (For other readers: if DOM is not an option, then please ignore this answer)
Please note that the
versionattribute of thexmlnode, refers to the version of the applied XML standard. Just to avoid confusion, it does not act as a revision number for the content of the document.That being said, you can use the
Document#getXmlVersionmethod Similarly, there is also agetXmlEncoding()andgetXmlStandalone()You could print them like this:
EDIT:
In answer to the question: "How to detect if version was specified or not".
The
versionattribute is stored in a private field inside theDocumentImpl, and internally it isnullwhen the version is not specified. In theorg.apache.xerces.domimplementation, the getter provides the default value:Unfortunately there are no getters to get the nullable form of the
versionfield. But you could access it using reflection: