tags:

views:

53

answers:

1

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 nth book(where n is any number). THank you

A: 

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.

McDowell
@McDowell I don't really get what kind of events are you talking about I just want to insert node in the document, if it was at the end or at the begining of the file I'd turn it into string and append it but I have a node constraint after which I should insert another node. thank you for answering
London
@London - You would insert the nodes by returning events created from a `XMLEventFactory`. If you provide some sample code showing how you're using StAX, it would be easier to provide relevant guidance.
McDowell