XML parsing with java dom parser converts xml declaration into elements
Here is the xml file that I need to process as part of my assignment.
<?开发者_JAVA百科xml-stylesheet type="text/xsl" href="people.xsl"?>
<People>
<Person>
`...`
</Person>
`...`
</People>
I am using the "javax.xml.parsers.DocumentBuilderFactory" to create a dom parser. After parsing, the resultant document does not have People at the root but some root having children as xml-stylesheet and People.
Looks like this can be avoided.
<?xml-stylesheet ... ?>
is not an XML declaration. It is a Processing Instruction (PI), and the DOM spec says that a Document node may contain zero or more of them.
One approach would be to code your application to deal appropriately with (e.g. ignore) an PI's in the Document node. Alternatively, just use the Document node's documentElement
attribute / getter to get the root Element directly.
精彩评论