I have a file which contains multiple sets of root elements. How can I extract the root element one by one?
This is my XML
<Person>
<empid></empid>
<name></name>
</Person>
<Person>
<empid></empid>
<name></name>
</Person>
<Person>
<empid></empid>
<name></name>
</Person>
How can I extract one set of Person
at a time?
You cannot parse your file using an XML parser because your file is not XML. XML cannot have more than one root element.
You have to treat it as text, repair it to be well-formed, and then you can parse it with an XML parser.
Use
java.io.SequenceInputStream
to trick xml parser:If your XML is valid, using a SAX or DOM parser. Please consult the XML Developer's Kit Programmer's Guide for more details.