views:

64

answers:

1

Here is the xml file that I need to process as part of my assignment.

<?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.

+1  A: 

<?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.

Stephen C
To clarify: nothing has been 'converted'. an XML DOM Document node can have multiple children. One of them is the root element. The rest are processing instructions or comments.
bmargulies