The fastest and most efficient way to create XML

What is the fastest and most efficient way to create XML documents in Java? There are many libraries (woodstox, xom, xstream ...), just wondering if anyone has any input. Should I go with a code generation approach (since the xml schema is well known)? Or a reflection approach at runtime?

Edited using additional information:

  • Well-defined XML schema is available and rarely changes
  • The requirement is to convert a Java object to XML, and not vice versa.
  • Thousands of java objects for XML per second
  • Code generation, code complexity, configuration, maintenance, etc. is second to higher performance.
+6
source share
4 answers

If I were to create very simple XML content , I would stick to the JDK api only, without introducing any third-party dependencies.

So, for plain XML, and if I were to map the XML file to Java classes (or vice versa), I would go for JAXB . See this tutorial to see how simple it is.

Now.

If I were to create more complex XML output with a constant schema, I would use some kind of template engine, Freemarker . Thymeleaf looks good.

And finally.

If I were to create huge XML files very efficiently, I would use the SAX parser.

I hope you now understand that you have many opportunities - choose the best fit for your needs :)

And have fun!

+11
source

Try Xembly , a small open source library that makes this XML creation 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 my own Java DOM and a very lightweight library (I'm a developer).

+4
source

First, it is important that serialization is correct. There are usually no handwritten serializers. For example, they tend to forget that the string "]]>" cannot be displayed in node text.

It's not difficult to write your own serializer, which is correct and fast if you are a compatible Java programmer, but since some very capable Java programmers were here before I think you are unlikely to beat enough of them to put in the effort to write own code.

In addition, it is possible that most general-purpose libraries may slow down a bit by offering serialization options β€” for example, indentation or coding, or how to choose line endings. You can simply squeeze an extra ounce of performance while avoiding unwanted features.

In addition, some general-purpose libraries can verify that you throw them at the correctness, for example, by checking that namespace prefixes are declared (or are declared if not). You can do it faster if it does not check. On the other hand, you can create a library that is fast, but a pig for work. Performance behavior above all other goals is almost always a mistake.

Regarding the performance of the available libraries, measure them and let us know what you learned.

+2
source

The best way I know is to use the XPath engine, which is able to create Nodes. XMLBeam can do this (in the JUnit test here):

  public interface Projection { @XBWrite("/create/some/xml/structure[@even='with Predicates']") void demo(String value); } @Test public void demo() { Projection projection = new XBProjector(Flags.TO_STRING_RENDERS_XML).projectEmptyDocument(Projection.class); projection.demo("Some value"); System.out.println(projection); } 

This program prints:

 <create> <some> <xml> <structure even="with Predicates">Some value</structure> </xml> </some> </create> 
+1
source

Source: https://habr.com/ru/post/907114/


All Articles