Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing an XML file without root in Java

I have this XML file which doesn't have a root node. Other than manually adding a "fake" root element, is there any way I would be able to parse an XML file in Java? Thanks.

like image 851
Seagull Avatar asked Jul 07 '10 02:07

Seagull


2 Answers

I suppose you could create a new implementation of InputStream that wraps the one you'll be parsing from. This implementation would return the bytes of the opening root tag before the bytes from the wrapped stream and the bytes of the closing root tag afterwards. That would be fairly simple to do.

I may be faced with this problem too. Legacy code, eh?

Ian.

Edit: You could also look at java.io.SequenceInputStream which allows you to append streams to one another. You would need to put your prefix and suffix in byte arrays and wrap them in ByteArrayInputStreams but it's all fairly straightforward.

like image 146
Ian Fairman Avatar answered Oct 04 '22 09:10

Ian Fairman


Your XML document needs a root xml element to be considered well formed. Without this you will not be able to parse it with an xml parser.

like image 22
krock Avatar answered Oct 04 '22 07:10

krock