batch xml generation

Questions about XML that are not covered by the other forums should go here.
johnbucannan
Posts: 1

batch xml generation

Tue Oct 07, 2014 4:54 am

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
Radu
Posts: 5221

Re: batch xml generation

Mon Dec 22, 2014 1:08 pm

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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests