Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest and most efficient way to create XML

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:

  1. Well defined XML Schema is available and rarely changes
  2. Requirement is to convert a java object to XML, and not vice versa
  3. Thousands of java objects to XML per second
  4. Code generation, code complexity, configuration, maintenance etc. is second to higher performance.
like image 416
arrehman Avatar asked Jan 28 '12 17:01

arrehman


People also ask

What programs create XML files?

Microsoft XML Notepad is an application that allows you to create and edit XML documents quickly and easily. With this tool, the structure of your XML data is displayed graphically in a tree structure.

Can Python create XML?

Creating XML Document using Python First, we import minidom for using xml. dom . Then we create the root element and append it to the XML. After that creating a child product of parent namely Geeks for Geeks.


2 Answers

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!

like image 176
ŁukaszBachman Avatar answered Sep 17 '22 14:09

ŁukaszBachman


Try Xembly, a small open source library that makes this XML creating process very easy and intuitive:

String xml = new Xembler(
  new Directives()
    .add("root")
    .add("order")
    .attr("id", "553")
    .set("$140.00")
).xml();

Xembly is a wrapper around native Java DOM, and is a very lightweight library (I'm a developer).

like image 36
yegor256 Avatar answered Sep 20 '22 14:09

yegor256