XMLTool – Java DSL for create XML

In Java, we usually create XML document through DOM API that come with standard JDK.

DOM API is good, but sometime the code we wrote with DOM API is verbose. Here is an example of the code required to generate a simple 8 lines XML. Yes, 13 line of Java code to generate a 8 lines XML.

Other options are using JDOM or dom4j that simplify DOM manipulation in Java.

If you just want a simple fluent API for create simple XML, XMLTool is another good option.

The sample code below quote from XMLTool Manual will demonstrate how to generate a XML with namespace via XMLTool’s fluent interface:

XMLTag doc = XMLDoc.newDocument()
    .addDefaultNamespace("http://www.w3.org/2002/06/xhtml2/")
    .addNamespace("wicket", "http://wicket.sourceforge.net/wicket-1.0")
    .addRoot("html")
    .addTag("wicket:border")
    .gotoRoot().addTag("head")
    .addNamespace("other", "http://other-ns.com")
    .gotoRoot().addTag("other:foo");

Will produce:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html xmlns="http://www.w3.org/2002/06/xhtml2/">
    <wicket:border xmlns:wicket="http://wicket.sourceforge.net/wicket-1.0"/>
    <head/>
    <other:foo xmlns:other="http://other-ns.com"/>
</html>

XMLTool is open sourced under Apache License 2.0. So feel free to give it a try.


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply