I need to read a large xml with VTD XML and XPath and split results in multiple nodes. I found some solutions here but it split nodes but without parents information.
What why I'm looking for:
XPath string: /CATALOG/MAIN/CD Based on XPath document should be splitted
1) Initial document:
<CATALOG>
<MAIN id="1">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
</CD>
<CD>
<TITLE>Empire Dummy</TITLE>
<ARTIST>John Doe</ARTIST>
</CD>
<USEFUL>Useful node</USEFUL>
</MAIN>
<MAIN id="2">
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary More</ARTIST>
</CD>
</MAIN>
<IGNORED>Ignored node</IGNORED>
</CATALOG>
2) Results: Document 1:
<CATALOG>
<MAIN id="1">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
</CD>
<USEFUL>Useful node</USEFUL>
</MAIN>
</CATALOG>
Document 2:
<CATALOG>
<MAIN id="1">
<CD>
<TITLE>Empire Dummy</TITLE>
<ARTIST>John Doe</ARTIST>
</CD>
<USEFUL>Useful node</USEFUL>
</MAIN>
</CATALOG>
Document 3:
<CATALOG>
<MAIN id="2">
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary More</ARTIST>
</CD>
</MAIN>
</CATALOG>
Thanks for your time and suggestions.
Best regards!
Herez my approach...
Use some xml parsing libraries e.g javax.xml.parsers.DocumentBuilderFactory Create a DOM for your input xml file... For each node encountered, create a new output file Document.xml e.g Document1.xml (add the child node under parent.
You might refer http://www.programcreek.com/java-api-examples/index.php?api=javax.xml.parsers.DocumentBuilderFactory for sample java code to parse xml using java.xml.parsers.* package (look for loadQuestions example)