0

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.

Unknown
  • 131
  • 1
  • 4
  • 11
  • If you already have the `String` representing your XML as intended, then you don't need to use StAX - just print it out to file. However if the XML is not well-formed, you might or might not be lucky with StAX. – Mena Nov 30 '15 at 17:04
  • Looks like duplicate of [How to pretty print XML from Java?](http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java) – Uday Shankar Nov 30 '15 at 17:13
  • I'll check this out more tomorrow Uday, I do need to make sure whatever method I use, its done in such a way that I can keep the file open, but flush the current stream into the .xml file due to the potential size of the file. I'll also need a way of injecting header information, at the start of the file, before writing a string, flushing the data, then coming back after additional processing to write more data to the same file, if this is possible VIA this method, then great. – Unknown Nov 30 '15 at 21:24

1 Answers1

0

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

Maybe a duplicate?

Community
  • 1
  • 1
Andreas Baaserud
  • 149
  • 3
  • 13
  • Hi, I did look at that, what I'm trying to do is different however. I'm writing to write an already defined .XML message (contained within a string) to a file on a hard disk, I don't want to write it to an object inbetween that process. But thanks for replying. – Unknown Nov 30 '15 at 17:09