What is the fastest and most efficient way to create XML documents in Java? There is a plethora of libraries out there (woodstox, xom, xstream…), just wondering if any one has any input. Should I go with code generation approach (since xml schema is well known)? Or reflection approach at run-time?
Edited with Additional information:
- Well defined XML Schema is available and rarely changes
- Requirement is to convert a java object to XML, and not vice versa
- Thousands of java objects to XML per second
- Code generation, code complexity, configuration, maintenance etc. is second to higher performance.
If I was to create a very simple XML content, I would stick to the JDK api only, introducing no third party dependencies.
So for simple XML and if I was to map XML file to Java classes (or vice-versa), I would go for JAXB. See this tutorial to see how easy it is.
Now.
If I was to create some more sophisticated XML output with constant scheme, I would use some templating engine, Freemarker perhaps. Thymeleaf looks nice as well.
And finally.
If I was to create huge XML files very effectively, I would use SAX parser.
I hope you understand now, that you have plenty of possibilities – choose the best match for your needs 🙂
And have fun!