I need to parse a continuous stream of well-formed XML elements, to which I am only given an already constructed java.io.Reader
object. These elements are not enclosed in a root element, nor are they prepended with an XML header like <?xml version="1.0"?>"
, but are otherwise valid XML.
Using the Java org.xml.sax.XMLReader
class does not work, because the XML Reader expects to parse well-formed XML, starting with an enclosing root element. So, it just reads the first element in the stream, which it perceives as the root, and fails in the next one, with the typical
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
For files that do not contain a root element, but where such element does exist or can be defined (and is called, say, MyRootElement), one can do something like the following:
Strint path = <the full path to the file>;
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
StringBuilder buffer = new StringBuilder();
buffer.append("<?xml version=\"1.0\"?>\n");
buffer.append("<!DOCTYPE MyRootElement ");
buffer.append("[<!ENTITY data SYSTEM \"file:///");
buffer.append(path);
buffer.append("\">]>\n");
buffer.append("<MyRootElement xmlns:...>\n");
buffer.append("&data;\n");
buffer.append("</MyRootElement>\n");
InputSource source = new InputSource(new StringReader(buffer.toString()));
xmlReader.parse(source);
I have tested the above by saving part of the java.io.Reader
output to a file and it works. However, this approach is not applicable in my case and such extra information (XML header, root element) cannot be inserted, since the java.io.Reader
object passed to my code is already constructed.
Essentially, I am looking for "fragmented XML parsing". So, my question is, can it be done, using standard Java APIs (including the org.sax.xml.*
and java.xml.*
packages)?
Just insert dummy root element. The most elegant solution I can think about is to create your own InputStream or Reader that wraps regular InputSteam/Reader and returns the dummy
<dummyroot>
when you call its read() / readLine() first time and then returns the result of payload stream. This should satisfy SAX parser.You can wrap your given
Reader
in aFilterReader
subclass that you implement to do more or less what you're doing here.Edit:
While this is similar to the proposal to implement your own
Reader
delegating to the givenReader
object given by a couple other answers, just about all methods inFilterReader
would have to be overridden, so you may not gain much from using the superclass.An interesting variation on the other proposals might be to implement a
SequencedReader
which wraps multipleReader
objects and shifts to the next in the sequence when one is used up. Then you could pass in aStringReader
object with the start text for the root you want to add, the originalReader
and anotherStringReader
with the closing tag.You can write your own Reader-Implementation that encapsulates the Reader-instance you're given. This new Reader should do just what you're doing in your example code, provide the header and root element, then the data from the underlying reader and in the end the closing root tag. By going this way you can provide a valid XML stream to the XML parser and you can as well use the Reader object passed to your code.
You can create your own Reader that delegates to the provided Reader, like this:
You'll have to fill in the logic to firstly read from
start
, then delegate to the reader in the middle, and finally when the reader is empty, read fromend
.This approach will work though.
Answer 3 works but for me I had to do the extra step of creating an inputsource from the SequenceInputStream.
SequenceInputStream comes to the rescue: