public final class XMLBuilder extends BaseXMLBuilder
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(Node myNode,
Node parentNode)
Construct a new builder object that wraps the given XML document and node.
|
| Modifier and Type | Method and Description |
|---|---|
XMLBuilder |
a(String name,
String value)
Synonym for
BaseXMLBuilder.attribute(String, String). |
XMLBuilder |
attr(String name,
String value)
Synonym for
BaseXMLBuilder.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
BaseXMLBuilder.comment(String). |
XMLBuilder |
cdata(byte[] data)
Add a CDATA node with Base64-encoded byte data content 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 |
cdata(String data)
Add a CDATA node with String content 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
BaseXMLBuilder.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.
|
static XMLBuilder |
create(String name,
String namespaceURI)
Construct a builder for new XML document with a default namespace.
|
XMLBuilder |
d(byte[] data)
Synonym for
BaseXMLBuilder.cdata(byte[]). |
XMLBuilder |
d(String data)
Synonym for
BaseXMLBuilder.cdata(String). |
XMLBuilder |
data(byte[] data)
Synonym for
BaseXMLBuilder.cdata(byte[]). |
XMLBuilder |
data(String data)
Synonym for
BaseXMLBuilder.cdata(String). |
XMLBuilder |
document()
BEWARE: The builder returned by this method represents a Document node, not
an Element node as is usually the case, so attempts to use the attribute or
namespace methods on this builder will likely fail.
|
XMLBuilder |
e(String name)
Synonym for
BaseXMLBuilder.element(String). |
XMLBuilder |
elem(String name)
Synonym for
BaseXMLBuilder.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.
|
XMLBuilder |
element(String name,
String namespaceURI)
Add a named and namespaced XML element to the document as a child of
this builder node, and return the builder node representing the new child.
|
XMLBuilder |
elementBefore(String name)
Add a named XML element to the document as a sibling element
that precedes the position of this builder node, and return the builder node
representing the new child.
|
XMLBuilder |
elementBefore(String name,
String namespaceURI)
Add a named and namespaced XML element to the document as a sibling element
that precedes the position of this builder node, and return the builder node
representing the new child.
|
XMLBuilder |
i(String target,
String data)
Synonym for
BaseXMLBuilder.instruction(String, String). |
XMLBuilder |
importXMLBuilder(BaseXMLBuilder builder)
Imports another XMLBuilder document into this document at the
current position.
|
XMLBuilder |
insertInstruction(String target,
String data)
Insert an instruction before the element represented by this builder node,
and return the node representing that same element
(not the new instruction node).
|
XMLBuilder |
inst(String target,
String data)
Synonym for
BaseXMLBuilder.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).
|
XMLBuilder |
namespace(String namespaceURI)
Add an XML namespace attribute to this builder's element node
without a prefix.
|
XMLBuilder |
namespace(String prefix,
String namespaceURI)
Add an XML namespace attribute to this builder's element node.
|
XMLBuilder |
ns(String namespaceURI)
Synonym for
BaseXMLBuilder.namespace(String). |
XMLBuilder |
ns(String prefix,
String namespaceURI)
Synonym for
BaseXMLBuilder.namespace(String, String). |
static XMLBuilder |
parse(File xmlFile)
Construct a builder from an existing XML document file.
|
static XMLBuilder |
parse(InputSource inputSource)
Construct a builder from an existing XML document.
|
static XMLBuilder |
parse(String xmlString)
Construct a builder from an existing XML document string.
|
XMLBuilder |
r(String name)
Synonym for
BaseXMLBuilder.reference(String). |
XMLBuilder |
ref(String name)
Synonym for
BaseXMLBuilder.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 |
stripWhitespaceOnlyTextNodes()
Find and delete from the underlying Document any text nodes that
contain nothing but whitespace, such as newlines and tab or space
characters used to indent or pretty-print an XML document.
|
XMLBuilder |
t(String value)
Synonym for
BaseXMLBuilder.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).
|
XMLBuilder |
text(String value,
boolean replaceText)
Add or replace the text value of an element represented by this builder
node, and return the node representing the element to which the text
was added (not the new text node).
|
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.
|
XMLBuilder |
xpathFind(String xpath,
NamespaceContext nsContext)
Find the first element in the builder's DOM matching the given
XPath expression, where the expression may include namespaces if
a
NamespaceContext is provided. |
assertElementContainsNoOrWhitespaceOnlyTextNodes, asString, asString, attributeImpl, buildDocumentNamespaceContext, cdataImpl, cdataImpl, commentImpl, createDocumentImpl, elementAsString, elementAsString, elementBeforeImpl, elementBeforeImpl, elementImpl, equals, getDocument, getElement, getPrefixFromQualifiedName, importXMLBuilderImpl, insertInstructionImpl, instructionImpl, lookupNamespaceURIImpl, namespaceImpl, namespaceImpl, parseDocumentImpl, referenceImpl, stripWhitespaceOnlyTextNodesImpl, textImpl, toWriter, toWriter, upImpl, xpathFindImpl, xpathQuery, xpathQueryprotected XMLBuilder(Document xmlDocument)
xmlDocument - an XML document that the builder will manage and manipulate.protected XMLBuilder(Node myNode, Node parentNode)
myNode - the XML node that this builder node will wrap. This node may
be part of the XML document, or it may be a new element that is to be
added to the document.parentNode - If not null, the given myElement will be appended as child node of the
parentNode node.public static XMLBuilder create(String name, String namespaceURI) throws ParserConfigurationException, FactoryConfigurationError
name - the name of the document's root element.namespaceURI - default namespace URI for document, ignored if null or empty.FactoryConfigurationErrorParserConfigurationExceptionpublic static XMLBuilder create(String name) throws ParserConfigurationException, FactoryConfigurationError
name - the name of the document's root element.FactoryConfigurationErrorParserConfigurationExceptionpublic static XMLBuilder parse(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException
inputSource - an XML document input source that will be parsed into a DOM.ParserConfigurationExceptionFactoryConfigurationErrorParserConfigurationExceptionIOExceptionSAXExceptionpublic static XMLBuilder parse(String xmlString) throws ParserConfigurationException, SAXException, IOException
xmlString - an XML document string that will be parsed into a DOM.ParserConfigurationExceptionFactoryConfigurationErrorParserConfigurationExceptionIOExceptionSAXExceptionpublic static XMLBuilder parse(File xmlFile) throws ParserConfigurationException, SAXException, IOException
xmlFile - an XML document file that will be parsed into a DOM.ParserConfigurationExceptionFactoryConfigurationErrorParserConfigurationExceptionIOExceptionSAXExceptionpublic XMLBuilder stripWhitespaceOnlyTextNodes() throws XPathExpressionException
BaseXMLBuilderstripWhitespaceOnlyTextNodes in class BaseXMLBuilderXPathExpressionExceptionpublic XMLBuilder importXMLBuilder(BaseXMLBuilder builder)
BaseXMLBuilderimportXMLBuilder in class BaseXMLBuilderbuilder - the XMLBuilder document to be imported.public XMLBuilder root()
root in class BaseXMLBuilderpublic XMLBuilder xpathFind(String xpath, NamespaceContext nsContext) throws XPathExpressionException
BaseXMLBuilderNamespaceContext is provided.xpathFind in class BaseXMLBuilderxpath - An XPath expression that *must* resolve to an existing Element within
the document object model.nsContext - a mapping of prefixes to namespace URIs that allows the XPath expression
to use namespaces.XPathExpressionException - If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE.public XMLBuilder xpathFind(String xpath) throws XPathExpressionException
BaseXMLBuilderxpathFind in class BaseXMLBuilderxpath - 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)
BaseXMLBuilderelement in class BaseXMLBuildername - the name of the XML element.public XMLBuilder elem(String name)
BaseXMLBuilderBaseXMLBuilder.element(String).elem in class BaseXMLBuildername - the name of the XML element.public XMLBuilder e(String name)
BaseXMLBuilderBaseXMLBuilder.element(String).e in class BaseXMLBuildername - the name of the XML element.public XMLBuilder element(String name, String namespaceURI)
BaseXMLBuilderelement in class BaseXMLBuildername - the name of the XML element.namespaceURI - a namespace URIpublic XMLBuilder elementBefore(String name)
BaseXMLBuilderelementBefore in class BaseXMLBuildername - the name of the XML element.public XMLBuilder elementBefore(String name, String namespaceURI)
BaseXMLBuilderelementBefore in class BaseXMLBuildername - the name of the XML element.namespaceURI - a namespace URIpublic XMLBuilder attribute(String name, String value)
BaseXMLBuilderattribute in class BaseXMLBuildername - the attribute's name.value - the attribute's value.public XMLBuilder attr(String name, String value)
BaseXMLBuilderBaseXMLBuilder.attribute(String, String).attr in class BaseXMLBuildername - the attribute's name.value - the attribute's value.public XMLBuilder a(String name, String value)
BaseXMLBuilderBaseXMLBuilder.attribute(String, String).a in class BaseXMLBuildername - the attribute's name.value - the attribute's value.public XMLBuilder text(String value, boolean replaceText)
BaseXMLBuildertext in class BaseXMLBuildervalue - the text value to set or add to the element.replaceText - if True any existing text content of the node is replaced with the
given text value, if the given value is appended to any existing text.public XMLBuilder text(String value)
BaseXMLBuildertext in class BaseXMLBuildervalue - the text value to add to the element.public XMLBuilder t(String value)
BaseXMLBuilderBaseXMLBuilder.text(String).t in class BaseXMLBuildervalue - the text value to add to the element.public XMLBuilder cdata(String data)
BaseXMLBuildercdata in class BaseXMLBuilderdata - the String value that will be added to a CDATA element.public XMLBuilder data(String data)
BaseXMLBuilderBaseXMLBuilder.cdata(String).data in class BaseXMLBuilderdata - the String value that will be added to a CDATA element.public XMLBuilder d(String data)
BaseXMLBuilderBaseXMLBuilder.cdata(String).d in class BaseXMLBuilderdata - the String value that will be added to a CDATA element.public XMLBuilder cdata(byte[] data)
BaseXMLBuildercdata in class BaseXMLBuilderdata - the data value that will be Base64-encoded and added to a CDATA element.public XMLBuilder data(byte[] data)
BaseXMLBuilderBaseXMLBuilder.cdata(byte[]).data in class BaseXMLBuilderdata - the data value that will be Base64-encoded and added to a CDATA element.public XMLBuilder d(byte[] data)
BaseXMLBuilderBaseXMLBuilder.cdata(byte[]).d in class BaseXMLBuilderdata - the data value that will be Base64-encoded and added to a CDATA element.public XMLBuilder comment(String comment)
BaseXMLBuildercomment in class BaseXMLBuildercomment - the comment to add to the element.public XMLBuilder cmnt(String comment)
BaseXMLBuilderBaseXMLBuilder.comment(String).cmnt in class BaseXMLBuildercomment - the comment to add to the element.public XMLBuilder c(String comment)
BaseXMLBuilderBaseXMLBuilder.comment(String).c in class BaseXMLBuildercomment - the comment to add to the element.public XMLBuilder instruction(String target, String data)
BaseXMLBuilderinstruction in class BaseXMLBuildertarget - the target value for the instruction.data - the data value for the instructionpublic XMLBuilder inst(String target, String data)
BaseXMLBuilderBaseXMLBuilder.instruction(String, String).inst in class BaseXMLBuildertarget - the target value for the instruction.data - the data value for the instructionpublic XMLBuilder i(String target, String data)
BaseXMLBuilderBaseXMLBuilder.instruction(String, String).i in class BaseXMLBuildertarget - the target value for the instruction.data - the data value for the instructionpublic XMLBuilder insertInstruction(String target, String data)
BaseXMLBuilderinsertInstruction in class BaseXMLBuildertarget - the target value for the instruction.data - the data value for the instructionpublic XMLBuilder reference(String name)
BaseXMLBuilderreference in class BaseXMLBuildername - the name value for the reference.public XMLBuilder ref(String name)
BaseXMLBuilderBaseXMLBuilder.reference(String).ref in class BaseXMLBuildername - the name value for the reference.public XMLBuilder r(String name)
BaseXMLBuilderBaseXMLBuilder.reference(String).r in class BaseXMLBuildername - the name value for the reference.public XMLBuilder namespace(String prefix, String namespaceURI)
BaseXMLBuildernamespace in class BaseXMLBuilderprefix - a prefix for the namespace URI within the document, may be null
or empty in which case a default "xmlns" attribute is created.namespaceURI - a namespace uripublic XMLBuilder ns(String prefix, String namespaceURI)
BaseXMLBuilderBaseXMLBuilder.namespace(String, String).ns in class BaseXMLBuilderprefix - a prefix for the namespace URI within the document, may be null
or empty in which case a default xmlns attribute is created.namespaceURI - a namespace uripublic XMLBuilder namespace(String namespaceURI)
BaseXMLBuildernamespace in class BaseXMLBuildernamespaceURI - a namespace uripublic XMLBuilder ns(String namespaceURI)
BaseXMLBuilderBaseXMLBuilder.namespace(String).ns in class BaseXMLBuildernamespaceURI - a namespace uripublic XMLBuilder up(int steps)
BaseXMLBuilderup in class BaseXMLBuildersteps - 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()
BaseXMLBuilderup in class BaseXMLBuilderpublic XMLBuilder document()
BaseXMLBuilderdocument in class BaseXMLBuilderCopyright © 2014. All Rights Reserved.