Assuming an XML file with unknown structure (i.e., unknown element and attribute names), like
<RootElement>
<Level 1 ...>
<Level 2 ...>
...
</Level 2>
<Level 2 ...>
...
</Level 2>
</Level 1>
<Level 1 ...>
<Level 2 ...>
...
</Level 2>
<Level 2 ...>
...
</Level 2>
</Level 1>
</RootElement>
Is there any way using StAX to get the full raw text of each element?
At least, how can this be done for the first level, i.e. in the above example (ignoring pretty printing) how can we read the following 2 strings in a Java String variable:
"<Level 1 ...><Level 2...>...</Level 2></Level 1>"
and
"<Level 1 ...><Level 2...>...</Level 2></Level 1>"
Use an XMLStreamReader and XMLStreamWriter together to get (producee) whatever raw XML you want to. It might seem like you can do some tricks for a more simple solution, but you can't - the XML needs to be parsed or else you are in deep water, and if you'd like to hack a parser, they are usually implemented with internal buffering which makes it a bit of hairy work to correctly cut up an incoming stream.
Edit:Use the parsing pattern in this question to keep track of the level. To write, handle each event type from the input in its own way - note that you can iterator over all the attributes and also namespaces for start element events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With