I am working on a simple XML parser in Java. I have been using DocumentBuilderFactory successfully with many sources, but one of my new ones is a collection of individual nodes.
The file.xml looks like this:
<XML Version....>
<!DOCTYPE...>
<main_document_node>
.....others....
</main_document_node>
<XML Version....>
<!DOCTYPE...>
<main_document_node>
.....others....
</main_document_node>
I had been using a command like this:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document d = dbf.newDocumentBuilder().parse(/path/file.xml);
But that doesn’t seem to work with multiple parent nodes. Is there an easier way than making a temporary working file that I write a main_document_node to? That pseudo-code would be
new Writer temp.xml
new Reader file.xml
while not at the end of file.xml
read/write first main_document_node to temp
parse temp.xml
I think there should be a way to use the inputsource/stream option of the DocumentBuilderFactory, but I’m not sure how to go about doing that.
Thanks!
You could skip the write to file step by creating an InputStream from the computet string fragment (for one node):