Small modification of XML document using StAX

I am currently trying to read in an XML file, make minor changes (change the value of some attributes) and write it again.

I wanted to use the StAX ( javax.xml.stream.XMLStreamReader) analyzer to read in each case, see if it was the one I wanted to change, and then pass it directly to the StAX ( javax.xml.stream.XMLStreamReader) record if any changes are required.

Unfortunately, this does not look so simple: the writer has no way to take the type of event and the parser object, only methods like this writeAttributeand writeStartElement. Obviously, I could write a large switch statement with an argument for every possible element type that might appear in an XML document, and just write it back, but it seems like a ton of problems for something similar, which should be simple.

Is there something I am missing that makes it easy to write a very similar XML document to the one you read using StAX?

+5
source share
3 answers

StAX . XML 20 . , SAX.

: , AFAIK - . API . API, , , .

+2

, , / , Stream.

(.. javax.xml.stream.XMLEventReader javax.xml.stream.XMLEventWriter)

. http://www.devx.com/tips/Tip/37795, , , .

+3

, , - - , : API- Woodstox Stax2 :

XMLStreamWriter2.copyEventFromReader(XMLStreamReader2 r, boolean preserveEventData) 

which copies the currently marked event from the stream reader using the stream recorder. It is not easy, but very effective. I used it for such changes with success.

(how to get XMLStreamWriter2, etc. All instances provided by Woodstox implement these extended versions - plus there are wrappers if someone wants to use the "basic" Stax variants, as well)

+2
source

All Articles