Skip to content
Advertisement

How to add XML elements from List into XMLEventWriter? Append XML to already created XMLEventWriter/XmlStreamWriter

I have a List<String> which contains the XML events created as a part of the output from the JAXB Marshaling approach. After completion of the JAXB Marshaling process this List<String> can contain large amounts of XML.

These XML fragments so are part of a large XML. The large XML has some additional header elements so I am trying to create the large XML using the XMLEventWriter and trying to add the elements from my LIST<String> but it does not work as expected and running into various errors.

I get the following error:

JavaScript

Following is the code I have:

JavaScript

Following is the output I am expecting:

JavaScript

I looked into XMLStreamWriter but I got to know that this cannot be done using that. I am not particular about XMLEventWriter. All I want to is get the required output using any of the approaches/libraries.

Can someone please help me with this? I just want to add the XML from my List to XMLEventWriter which has been created already with few Nodes.

Advertisement

Answer

First, your ending events are wrong:

JavaScript

They should be:

JavaScript

Second, the issue is that the xer event reader ends with an “End Document” event that you don’t want copied, so you need to filter it out.

To do that, wrap xer with a delegate that ends the event stream when the “End Document” event is reached:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement