public class XmlBuilder extends Object
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 and Description |
---|
XmlBuilder() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static ElementNode element(String nsUri, String qname, Node... children)
nsUri
- The namespace URI; ignored if null.qname
- Qualified name of the element.children
- Any children to be added to the element.public static ElementNode element(String name, Node... children)
name
- Name of the element.children
- Any children to be added to the element.public static Node attribute(String nsUri, String qname, String value)
nsUri
- The namespace URI; ignored if null.qname
- Qualified name of the attribute.value
- Value of the attribute.public static Node attribute(String name, String value)
name
- Name of the attribute.value
- Value of the attribute.public static Node comment(String text)
Warning:
Comment nodes are only reported to SAX content handlers that also
implement org.xml.sax.ext.LexicalHandler
.