Take the 2-minute tour ×

I'm using java 6 and processing some xml documents which are pretty large...I need to parse them and modify some values and then serialize back to the disk.

I used org.w3c.DOM to deserialize the xml documents and modified some attribute values, and I used JAXP Transformer to serialize the dom document that has been changed. But I found that it is really slow...

So I'm wondering is there a more efficient way to serialize the dom document, or handle large xml documents?


I used a timer to record how long it takes for each part, below is for the serialization:

// serialize the updated DOM
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

long t0 = timer.currentTimeMillis();
DOMSource source = new DOMSource(dom);
StreamResult result = new StreamResult(doc);
transformer.transform(source, result);
long t1 = timer.currentTimeMillis();

Reporter.log("Finished serializing " + doc.getAbsolutePath() + " in " + (((t1 - t0)) / 1000.0f) + " s.", true);

And the log shows:

Finished serializing C:\Usrs\Adminstrator\Documents\Docs\InitialDocument_1.xml in 53 s.
share|improve this question
How large is "large"? 10K? 100K? Several gigabytes? –  Timo Geusch May 6 '13 at 19:14
add comment

4 Answers

up vote 3 down vote accepted

You should consider using StAX. DOM is not suitable here. You can see the comparison here.


You can refer to the below URL for sample code.


share|improve this answer
Hi @dkaustubh, thank you for your answer! I used a timer to record how long each part takes (e.g. the converting from xml to dom, the parsing dom, the serialization). The serialization takes about 50 seconds on a 90KB file, which doesn't make any sense to me...I don't know what is wrong with the serialization part... –  D.Q. May 6 '13 at 19:34
@D.Q. Can you please share your code with timers? –  dkaustubh May 6 '13 at 19:37
please see the update of the post. –  D.Q. May 6 '13 at 19:52
stax is not well suited, it doesn't build object model, and is somewaht hard to use –  vtd-xml-author May 14 '13 at 19:47
add comment

The fastest way is StAX. The simplest way is JAXB.

share|improve this answer
vtd-xml is actually way faster than StAx, it is not even close –  vtd-xml-author May 14 '13 at 19:42
add comment

have you tried using the SAX interface?

If you need really fast processing of very large xml documents , you have to eschew DOM structure. Take a look at the non-dom parsers like this:


share|improve this answer
add comment

50 seconds to serialize 90Kb is crazy. DOM is slow, but not that slow; there's something going wrong and I can't tell what.

It's seriously misleading to describe 90Kb as "large", however, and that misdescription may have influenced some of the answers.

How fast do you need it to be? My guess is that the standard transformation mechanisms such as XSLT are quite fast enough.

The other relevant factor is, what exactly are the changes you need to make to the content? A lot depends on the complexity of the logic needed.

share|improve this answer
add comment

Your Answer


