net.sf.practicalxml.builder
Class XmlBuilder

java.lang.Object
  extended by net.sf.practicalxml.builder.XmlBuilder

public class XmlBuilder
extends Object

A tool for building XML, avoiding the mistakes of simple text output and the hassle of the DOM API. Primarily intended for building small snippets of XML for unit tests, but usable for all types of output.

This class takes a declarative approach to creating XML, using static factory methods to create Node objects. These are a lightweight representation of the DOM Node, focused on creation rather than manipulation. You can also append children to nodes imperatively, using the methods in this class to create the node objects.

This is best explained by example. To create the following XML:

  
You would have a program that looks like this:
  


Constructor Summary
XmlBuilder()
           
 
Method Summary
static Node attribute(String name, String value)
          Creates an attribute without namespace.
static Node attribute(String nsUri, String qname, String value)
          Creates a namespaced attribute.
static Node comment(String text)
          Creates a comment node.
static ElementNode element(String name, Node... children)
          Creates an element that does not have a namespace.
static ElementNode element(String nsUri, String qname, Node... children)
          Creates an element, with optional namespace.
static Node processingInstruction(String target, String data)
          Creates a processing instruction node.
static Node text(String content)
          Creates a text node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlBuilder

public XmlBuilder()
Method Detail

element

public static ElementNode element(String nsUri,
                                  String qname,
                                  Node... children)
Creates an element, with optional namespace.

Parameters:
nsUri - The namespace URI; ignored if null.
qname - Qualified name of the element.
children - Any children to be added to the element.

element

public static ElementNode element(String name,
                                  Node... children)
Creates an element that does not have a namespace.

Parameters:
name - Name of the element.
children - Any children to be added to the element.

text

public static Node text(String content)
Creates a text node.


attribute

public static Node attribute(String nsUri,
                             String qname,
                             String value)
Creates a namespaced attribute.

Parameters:
nsUri - The namespace URI; ignored if null.
qname - Qualified name of the attribute.
value - Value of the attribute.

attribute

public static Node attribute(String name,
                             String value)
Creates an attribute without namespace.

Parameters:
name - Name of the attribute.
value - Value of the attribute.

comment

public static Node comment(String text)
Creates a comment node.

Warning: Comment nodes are only reported to SAX content handlers that also implement org.xml.sax.ext.LexicalHandler.


processingInstruction

public static Node processingInstruction(String target,
                                         String data)
Creates a processing instruction node.

Since:
1.0.2