I already understand from reading different threads that generating your own XML string using a StringBuilder
is looked down upon, but the reason usually comes down to escaping characters.
I’d like to know if anyone is experienced with XML libraries and writing XML using StringBuilders and know if there is a big difference in performance to writing an XML which might be 2GB of data. The important thing to understand is that this XML will ALWAYS contain the same 6 elements, one of which is repeated for n
amount of times. Here n
can be thousands of times. What we are worried about mostly is performance when generating very large XML using java libraries like JaxB. Currently I’m trusting the person saying performance is an issue with large XML files using JaxB for example, but I’m wondering if we’re making the right choice by using StringBuilder instead. Is anyone experienced with either?
Advertisement
Answer
Don’t use JAXB for this, but do use a serialization library that allows you to output events such as startElement(), text(), endElement(). Even if you write your own library on top of a StringBuilder! The performance cost will be negligible, the time you save in debugging will be invaluable. And you won’t have the embarrassment of being one of those people who ship ill-formed XML to your customers, leading to innumerable questions on SO about how to repair badly-generated XML.