Page 1 of 1

batch xml generation

Posted: Tue Oct 07, 2014 4:54 am
by johnbucannan
I have a requirement where I read data from database and form list of objects. The count of the objects can go upto 500000. I want to serialize these objects into an xml file but also make sure the size of the file doesn't surpass certain size for example 10 MB. As soon as the size reaches around 10 mb i start serializing into another file.I have to also make sure the xml formed is welformed. Apart from these objects there are few tags in the xml which will be common between files. Below is a sample of what I want:

File1

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<batch>
<constant1>one</constant1>
<constant2>two</constant2>
<constant3>three</constant3>
<data>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
</data>

</batch>
File2

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<batch>
<constant1>one</constant1>
<constant2>two</constant2>
<constant3>three</constant3>
<data>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
<d>aaaaaaaaaaa</d>
</data>

</batch>
Constant1, constant2, constant3 are common between files but elements inside data are the actual objects from DB
Need help to see how can i achieve this using java.Also, which xml generation way would be ideal?Tree based, stream or JAXB.

Thankyou

Re: batch xml generation

Posted: Mon Dec 22, 2014 1:08 pm
by Radu
Hi John,

This can probably be done in several ways, possibly avoiding building a DOM structure and serializing it if the XML is quite large (DOM takes in memory about 5-6 times more than the XML structure takes on disk).
You could for example use javax.xml.transform functionality like:

Code: Select all

    TransformerFactory tf = TransformerFactory.newInstance();
final SAXTransformerFactory stf = (SAXTransformerFactory) tf;
TransformerHandler handler = stf.newTransformerHandler();
handler.setResult(new StreamResult(new File("test.xml")));
handler.startDocument();
handler.startElement("", "elemName", "elemName", null);
String text = "abc";
handler.characters(text.toCharArray(), 0, text.length());
handler.endElement("", "elemName", "elemName");
handler.endDocument();
Regards,
Radu