Java - stAX - Creating .xml from String

609 Views Asked by At

I need to create a singular, and potentially very large .xml file and my research has made me aware of stAX. I've spent a while trying to get my head around it, but I'm not really sure if its really the right tool for the job.

So basicly, I've got a single String, this string contains an formatted XML message. So from this message I need to be able to write this as a well formed .xml file. Here is an example XML string: (Not complete, the final String is much more complex)

<RISKADDRESS><ASINPUT><HOUSENR/><HOUSENAME/><STREET1>86 Warren Avenue</STREET1><STREET2/><DISTRICT/><CITY>Southampton</CITY><COUNTY>GB</COUNTY><POSTCODE>SO16 6AE</POSTCODE><COUNTRY>GB</COUNTRY></ASINPUT></RISKADDRESS>

Just as a sidenote, I can also get my data as a HashMap.

Can anyone point me in the right direction, what are the steps involved in this process? All of the tutorials I've come across so far have not really shed any light on my issue. Below is an example of where I've got to, before getting stuck.

XMLOutputFactory factory = XMLOutputFactory.newInstance ();
XMLStreamWriter writer = new IndentingXMLStreamWriter (factory.createXMLStreamWriter (out));

writer.writeStartDocument ("UTF-8", "1.0");

//Do i have to iterate over my String in some way here?


writer.writeEndDocument ();

writer.flush ();
writer.close ();

Edit: I should add that I ideally need to make this code reusable, so specifying the exact attribute names and so on isn't really an option.

1

There are 1 best solutions below

1
On

I cant create any comment, but did you check out this question? how to parse xml to java object?

Maybe a duplicate?