public class XMLBuilder extends Object
Internally, XML Builder uses JAXP to build a standard W3C
Document
model (DOM) that you can easily export as a
string, or access and manipulate further if you have special requirements.
The XMLBuilder class serves as a wrapper of Element
nodes,
and provides a number of utility methods that make it simple to
manipulate the underlying element and the document to which it belongs.
In essence, this class performs dual roles: it represents a specific XML
node, and also allows manipulation of the entire underlying XML document.
The platform's default DocumentBuilderFactory
and
DocumentBuilder
classes are used to build the document.
Modifier | Constructor and Description |
---|---|
protected |
XMLBuilder(Document xmlDocument)
Construct a new builder object that wraps the given XML document.
|
protected |
XMLBuilder(Element myElement,
Element parentElement)
Construct a new builder object that wraps the given XML document
and element element.
|
Modifier and Type | Method and Description |
---|---|
XMLBuilder |
a(String name,
String value)
Synonym for
attribute(String, String) . |
protected void |
assertCurrentElementHasNoTextNodes() |
String |
asString()
Serialize the XML document to a string excluding the XML declaration.
|
String |
asString(Properties outputProperties)
Serialize the XML document to a string by delegating to the
toWriter(Writer, Properties) method. |
XMLBuilder |
attr(String name,
String value)
Synonym for
attribute(String, String) . |
XMLBuilder |
attribute(String name,
String value)
Add a named attribute value to the element represented by this builder
node, and return the node representing the element to which the
attribute was added (not the new attribute node).
|
XMLBuilder |
c(String comment)
Synonym for
comment(String) . |
XMLBuilder |
cdata(byte[] data)
Add a CDATA value to the element represented by this builder node, and
return the node representing the element to which the data
was added (not the new CDATA node).
|
XMLBuilder |
cmnt(String comment)
Synonym for
comment(String) . |
XMLBuilder |
comment(String comment)
Add a comment to the element represented by this builder node, and
return the node representing the element to which the comment
was added (not the new comment node).
|
static XMLBuilder |
create(String name)
Construct a builder for new XML document.
|
XMLBuilder |
d(byte[] data)
Synonym for
#cdata(String) . |
XMLBuilder |
data(byte[] data)
Synonym for
#cdata(String) . |
XMLBuilder |
e(String name)
Synonym for
element(String) . |
XMLBuilder |
elem(String name)
Synonym for
element(String) . |
XMLBuilder |
element(String name)
Add a named XML element to the document as a child of this builder node,
and return the builder node representing the new child.
|
boolean |
equals(Object obj) |
Document |
getDocument() |
Element |
getElement() |
XMLBuilder |
i(String target,
String data)
Synonym for
instruction(String, String) . |
XMLBuilder |
importXMLBuilder(XMLBuilder builder)
Imports another XMLBuilder document into this document at the
current position.
|
XMLBuilder |
inst(String target,
String data)
Synonym for
instruction(String, String) . |
XMLBuilder |
instruction(String target,
String data)
Add an instruction to the element represented by this builder node, and
return the node representing the element to which the instruction
was added (not the new instruction node).
|
static XMLBuilder |
parse(InputSource inputSource)
Construct a builder from an existing XML document.
|
XMLBuilder |
r(String name)
Synonym for
reference(String) . |
XMLBuilder |
ref(String name)
Synonym for
reference(String) . |
XMLBuilder |
reference(String name)
Add a reference to the element represented by this builder node, and
return the node representing the element to which the reference
was added (not the new reference node).
|
XMLBuilder |
root() |
XMLBuilder |
t(String value)
Synonmy for
text(String) . |
XMLBuilder |
text(String value)
Add a text value to the element represented by this builder node, and
return the node representing the element to which the text
was added (not the new text node).
|
void |
toWriter(Writer writer,
Properties outputProperties)
Serialize the XML document to the given writer using the default
TransformerFactory and Transformer classes. |
XMLBuilder |
up()
Return the builder node representing the parent of the current node.
|
XMLBuilder |
up(int steps)
Return the builder node representing the nth ancestor element
of this node, or the root node if n exceeds the document's depth.
|
XMLBuilder |
xpathFind(String xpath)
Find the first element in the builder's DOM matching the given
XPath expression.
|
protected XMLBuilder(Document xmlDocument)
xmlDocument
- an XML document that the builder will manage and manipulate.protected XMLBuilder(Element myElement, Element parentElement)
myElement
- the XML element that this builder node will wrap. This element may
be part of the XML document, or it may be a new element that is to be
added to the document.parentElement
- If not null, the given myElement will be appended as child node of the
parentElement node.public static XMLBuilder create(String name) throws ParserConfigurationException, FactoryConfigurationError
name
- the name of the document's root element.FactoryConfigurationError
ParserConfigurationException
public static XMLBuilder parse(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException
inputSource
- an XML document input source that will be parsed into a DOM.ParserConfigurationException
FactoryConfigurationError
ParserConfigurationException
IOException
SAXException
public XMLBuilder importXMLBuilder(XMLBuilder builder)
builder
- the XMLBuilder document to be imported.public boolean equals(Object obj)
public Element getElement()
public XMLBuilder root()
create(String)
or parse(InputSource)
method.public Document getDocument()
public XMLBuilder xpathFind(String xpath) throws XPathExpressionException
xpath
- An XPath expression that *must* resolve to an existing Element within
the document object model.XPathExpressionException
- If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE
.public XMLBuilder element(String name)
name
- the name of the XML element.IllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public XMLBuilder elem(String name)
element(String)
.name
- the name of the XML element.IllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public XMLBuilder e(String name)
element(String)
.name
- the name of the XML element.IllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public XMLBuilder attribute(String name, String value)
name
- the attribute's name.value
- the attribute's value.public XMLBuilder attr(String name, String value)
attribute(String, String)
.name
- the attribute's name.value
- the attribute's value.public XMLBuilder a(String name, String value)
attribute(String, String)
.name
- the attribute's name.value
- the attribute's value.public XMLBuilder text(String value)
value
- the text value to add to the element.public XMLBuilder t(String value)
text(String)
.value
- the text value to add to the element.public XMLBuilder cdata(byte[] data)
data
- the data value that will be Base64-encoded and added to a CDATA element.public XMLBuilder data(byte[] data)
#cdata(String)
.data
- the data value to add to the element.public XMLBuilder d(byte[] data)
#cdata(String)
.data
- the data value to add to the element.public XMLBuilder comment(String comment)
comment
- the comment to add to the element.public XMLBuilder cmnt(String comment)
comment(String)
.comment
- the comment to add to the element.public XMLBuilder c(String comment)
comment(String)
.comment
- the comment to add to the element.public XMLBuilder instruction(String target, String data)
target
- the target value for the instruction.data
- the data value for the instructionpublic XMLBuilder inst(String target, String data)
instruction(String, String)
.target
- the target value for the instruction.data
- the data value for the instructionpublic XMLBuilder i(String target, String data)
instruction(String, String)
.target
- the target value for the instruction.data
- the data value for the instructionpublic XMLBuilder reference(String name)
name
- the name value for the reference.public XMLBuilder ref(String name)
reference(String)
.name
- the name value for the reference.public XMLBuilder r(String name)
reference(String)
.name
- the name value for the reference.public XMLBuilder up(int steps)
steps
- the number of parent elements to step over while navigating up the chain
of node ancestors. A steps value of 1 will find a node's parent, 2 will
find its grandparent etc.public XMLBuilder up()
protected void assertCurrentElementHasNoTextNodes()
IllegalStateException
- if the current element contains any child text nodes.public void toWriter(Writer writer, Properties outputProperties) throws TransformerException
TransformerFactory
and Transformer
classes. If output
options are provided, these options are provided to the
Transformer
serializer.writer
- a writer to which the serialized document is written.outputProperties
- settings for the Transformer
serializer. This parameter may be
null or an empty Properties object, in which case the default output
properties will be applied.TransformerException
public String asString(Properties outputProperties) throws TransformerException
toWriter(Writer, Properties)
method. If output options are
provided, these options are provided to the Transformer
serializer.outputProperties
- settings for the Transformer
serializer. This parameter may be
null or an empty Properties object, in which case the default output
properties will be applied.TransformerException
public String asString() throws TransformerException
TransformerException
Copyright © 2014. All rights reserved.