Well, itβs pretty clear why he behaves like him. What you are actually doing is opening an existing file in the mode of adding and writing items at the end. This is clearly contrary to what you are trying to do.
(In addition: I am surprised that it works as well as if the input side is likely to see elements added by the output side to the end of the file. Indeed, exceptions such as the example of Evgeny Dorofeev give that I expect. The problem is that if you try to read and write a text file at the same time, and either the reader or the writer uses any form of buffering, explicit or implicit, the reader is responsible for viewing partial states.)
To fix this, you need to start by reading from one file and writing to another file. Adding will not work. Then you have to consider that the elements, attributes, contents, etc. that are read from the input file are copied to the output file. Finally, you need to add additional elements to the corresponding points.
And is it possible to open an XML file in a mode such as RandomAccessFile, but write StAX methods in it?
Not. This is theoretically impossible. To be able to move around the XML file structure in a "random" file, you first need to analyze all this and build an index where all the elements are located. Even when you have done this, XML is still saved as characters in the file, and random access does not allow you to insert or delete characters in the middle of the file.
Perhaps your best bet would be to combine XSL and the SAX parser; for example something like this IBM article: http://ibm.com/developerworks/xml/library/x-tiptrax
Stephen c
source share