How to add node to existing XML Document
I'm using stax to parse my document, here is a dummy example of my XML document :
<data>
<video>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</video>
<book id="1">
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
<book id="n">
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
//insert new node here
</data>
How can I insert node after nt开发者_如何转开发h book(where n is any number). THank you
Decorate your XMLEventReader from the factory with your own implementation that provides your own events at the appropriate time. You might extend EventReaderDelegate to do this. You'd queue up the events you want to insert, count the elements read and provide them at the appropriate time.
If you're using XMLStreamReader, the same principle applies but there's a few more methods to take care of.
精彩评论