Class XMLBuilder2
- java.lang.Object
-
- com.jamesmurty.utils.BaseXMLBuilder
-
- com.jamesmurty.utils.XMLBuilder2
-
public final class XMLBuilder2 extends BaseXMLBuilder
XML Builder is a utility that creates simple XML documents using relatively sparse Java code. It is intended to allow for quick and painless creation of XML documents where you might otherwise be tempted to use concatenated strings, rather than face the tedium and verbosity of coding with JAXP (http://jaxp.dev.java.net/).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 XMLBuilder2 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 defaultDocumentBuilderFactory
andDocumentBuilder
classes are used to build the document.XMLBuilder2 has an feature set to the original XMLBuilder, but only ever throws runtime exceptions (as opposed to checked exceptions). Any internal checked exceptions are caught and wrapped in an
XMLBuilderRuntimeException
object.- Author:
- James Murty
-
-
Field Summary
-
Fields inherited from class com.jamesmurty.utils.BaseXMLBuilder
failIfExternalEntityParsingCannotBeConfigured
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
XMLBuilder2(Document xmlDocument)
Construct a new builder object that wraps the given XML document.protected
XMLBuilder2(Node myNode, Node parentNode)
Construct a new builder object that wraps the given XML document and node.
-
Method Summary
Modifier and Type Method Description XMLBuilder2
a(String name, String value)
Synonym forBaseXMLBuilder.attribute(String, String)
.String
asString()
Serialize the XML document to a string excluding the XML declaration.String
asString(Properties properties)
Serialize the XML document to a string by delegating to theBaseXMLBuilder.toWriter(Writer, Properties)
method.XMLBuilder2
attr(String name, String value)
Synonym forBaseXMLBuilder.attribute(String, String)
.XMLBuilder2
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).XMLBuilder2
c(String comment)
Synonym forBaseXMLBuilder.comment(String)
.XMLBuilder2
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).XMLBuilder2
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).XMLBuilder2
cmnt(String comment)
Synonym forBaseXMLBuilder.comment(String)
.XMLBuilder2
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 XMLBuilder2
create(String name)
Construct a builder for new XML document.static XMLBuilder2
create(String name, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder for new XML document.static XMLBuilder2
create(String name, String namespaceURI)
Construct a builder for new XML document with a default namespace.static XMLBuilder2
create(String name, String namespaceURI, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder for new XML document with a default namespace.XMLBuilder2
d(byte[] data)
Synonym forBaseXMLBuilder.cdata(byte[])
.XMLBuilder2
d(String data)
Synonym forBaseXMLBuilder.cdata(String)
.XMLBuilder2
data(byte[] data)
Synonym forBaseXMLBuilder.cdata(byte[])
.XMLBuilder2
data(String data)
Synonym forBaseXMLBuilder.cdata(String)
.XMLBuilder2
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.XMLBuilder2
e(String name)
Synonym forBaseXMLBuilder.element(String)
.XMLBuilder2
elem(String name)
Synonym forBaseXMLBuilder.element(String)
.XMLBuilder2
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.XMLBuilder2
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.String
elementAsString()
Serialize the current XML Element and its descendants to a string excluding the XML declaration.String
elementAsString(Properties outputProperties)
Serialize the current XML Element and its descendants to a string by delegating to theBaseXMLBuilder.toWriter(Writer, Properties)
method.XMLBuilder2
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.XMLBuilder2
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.XMLBuilder2
i(String target, String data)
Synonym forBaseXMLBuilder.instruction(String, String)
.XMLBuilder2
importXMLBuilder(BaseXMLBuilder builder)
Imports another XMLBuilder document into this document at the current position.XMLBuilder2
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).XMLBuilder2
inst(String target, String data)
Synonym forBaseXMLBuilder.instruction(String, String)
.XMLBuilder2
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).XMLBuilder2
namespace(String namespaceURI)
Add an XML namespace attribute to this builder's element node without a prefix.XMLBuilder2
namespace(String prefix, String namespaceURI)
Add an XML namespace attribute to this builder's element node.XMLBuilder2
ns(String namespaceURI)
Synonym forBaseXMLBuilder.namespace(String)
.XMLBuilder2
ns(String prefix, String namespaceURI)
Synonym forBaseXMLBuilder.namespace(String, String)
.static XMLBuilder2
parse(File xmlFile)
Construct a builder from an existing XML document file.static XMLBuilder2
parse(File xmlFile, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder from an existing XML document file.static XMLBuilder2
parse(String xmlString)
Construct a builder from an existing XML document string.static XMLBuilder2
parse(String xmlString, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder from an existing XML document string.static XMLBuilder2
parse(InputSource inputSource)
Construct a builder from an existing XML document.static XMLBuilder2
parse(InputSource inputSource, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder from an existing XML document.XMLBuilder2
r(String name)
Synonym forBaseXMLBuilder.reference(String)
.XMLBuilder2
ref(String name)
Synonym forBaseXMLBuilder.reference(String)
.XMLBuilder2
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).XMLBuilder2
root()
XMLBuilder2
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.XMLBuilder2
t(String value)
Synonym forBaseXMLBuilder.text(String)
.XMLBuilder2
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).XMLBuilder2
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).void
toWriter(boolean wholeDocument, Writer writer, Properties outputProperties)
Serialize either the specific Element wrapped by this BaseXMLBuilder, or its entire XML document, to the given writer using the defaultTransformerFactory
andTransformer
classes.void
toWriter(Writer writer, Properties outputProperties)
Serialize the XML document to the given writer using the defaultTransformerFactory
andTransformer
classes.XMLBuilder2
up()
Return the builder node representing the parent of the current node.XMLBuilder2
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.XMLBuilder2
xpathFind(String xpath)
Find the first element in the builder's DOM matching the given XPath expression.XMLBuilder2
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 aNamespaceContext
is provided.Object
xpathQuery(String xpath, QName type)
Return the result of evaluating an XPath query on the builder's DOM.Object
xpathQuery(String xpath, QName type, NamespaceContext nsContext)
Return the result of evaluating an XPath query on the builder's DOM using the given namespace.-
Methods inherited from class com.jamesmurty.utils.BaseXMLBuilder
assertElementContainsNoOrWhitespaceOnlyTextNodes, attributeImpl, buildDocumentNamespaceContext, cdataImpl, cdataImpl, commentImpl, createDocumentImpl, elementBeforeImpl, elementBeforeImpl, elementImpl, enableOrDisableExternalEntityParsing, equals, getDocument, getElement, getPrefixFromQualifiedName, importXMLBuilderImpl, insertInstructionImpl, instructionImpl, lookupNamespaceURIImpl, namespaceImpl, namespaceImpl, parseDocumentImpl, referenceImpl, stripWhitespaceOnlyTextNodesImpl, textImpl, upImpl, xpathFindImpl
-
-
-
-
Constructor Detail
-
XMLBuilder2
protected XMLBuilder2(Document xmlDocument)
Construct a new builder object that wraps the given XML document. This constructor is for internal use only.- Parameters:
xmlDocument
- an XML document that the builder will manage and manipulate.
-
XMLBuilder2
protected XMLBuilder2(Node myNode, Node parentNode)
Construct a new builder object that wraps the given XML document and node. This constructor is for internal use only.- Parameters:
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.
-
-
Method Detail
-
create
public static XMLBuilder2 create(String name, String namespaceURI, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder for new XML document with a default namespace. The document will be created with the given root element, and the builder returned by this method will serve as the starting-point for any further document additions.- Parameters:
name
- the name of the document's root element.namespaceURI
- default namespace URI for document, ignored if null or empty.enableExternalEntities
- enable external entities; beware of XML External Entity (XXE) injection.isNamespaceAware
- enable or disable namespace awareness in the underlyingDocumentBuilderFactory
- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
-
create
public static XMLBuilder2 create(String name, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder for new XML document. The document will be created with the given root element, and the builder returned by this method will serve as the starting-point for any further document additions.- Parameters:
name
- the name of the document's root element.enableExternalEntities
- enable external entities; beware of XML External Entity (XXE) injection.isNamespaceAware
- enable or disable namespace awareness in the underlyingDocumentBuilderFactory
- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
-
create
public static XMLBuilder2 create(String name, String namespaceURI)
Construct a builder for new XML document with a default namespace. The document will be created with the given root element, and the builder returned by this method will serve as the starting-point for any further document additions.- Parameters:
name
- the name of the document's root element.namespaceURI
- default namespace URI for document, ignored if null or empty.- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
-
create
public static XMLBuilder2 create(String name)
Construct a builder for new XML document. The document will be created with the given root element, and the builder returned by this method will serve as the starting-point for any further document additions.- Parameters:
name
- the name of the document's root element.- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
-
parse
public static XMLBuilder2 parse(InputSource inputSource, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder from an existing XML document. The provided XML document will be parsed and an XMLBuilder2 object referencing the document's root element will be returned.- Parameters:
inputSource
- an XML document input source that will be parsed into a DOM.enableExternalEntities
- enable external entities; beware of XML External Entity (XXE) injection.isNamespaceAware
- enable or disable namespace awareness in the underlyingDocumentBuilderFactory
- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
,SAXException
,IOException
-
parse
public static XMLBuilder2 parse(String xmlString, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder from an existing XML document string. The provided XML document will be parsed and an XMLBuilder2 object referencing the document's root element will be returned.- Parameters:
xmlString
- an XML document string that will be parsed into a DOM.enableExternalEntities
- enable external entities; beware of XML External Entity (XXE) injection.isNamespaceAware
- enable or disable namespace awareness in the underlyingDocumentBuilderFactory
- Returns:
- a builder node that can be used to add more nodes to the XML document.
-
parse
public static XMLBuilder2 parse(File xmlFile, boolean enableExternalEntities, boolean isNamespaceAware)
Construct a builder from an existing XML document file. The provided XML document will be parsed and an XMLBuilder2 object referencing the document's root element will be returned.- Parameters:
xmlFile
- an XML document file that will be parsed into a DOM.enableExternalEntities
- enable external entities; beware of XML External Entity (XXE) injection.isNamespaceAware
- enable or disable namespace awareness in the underlyingDocumentBuilderFactory
- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
,SAXException
,IOException
,FileNotFoundException
-
parse
public static XMLBuilder2 parse(InputSource inputSource)
Construct a builder from an existing XML document. The provided XML document will be parsed and an XMLBuilder2 object referencing the document's root element will be returned.- Parameters:
inputSource
- an XML document input source that will be parsed into a DOM.- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
,SAXException
,IOException
-
parse
public static XMLBuilder2 parse(String xmlString)
Construct a builder from an existing XML document string. The provided XML document will be parsed and an XMLBuilder2 object referencing the document's root element will be returned.- Parameters:
xmlString
- an XML document string that will be parsed into a DOM.- Returns:
- a builder node that can be used to add more nodes to the XML document.
-
parse
public static XMLBuilder2 parse(File xmlFile)
Construct a builder from an existing XML document file. The provided XML document will be parsed and an XMLBuilder2 object referencing the document's root element will be returned.- Parameters:
xmlFile
- an XML document file that will be parsed into a DOM.- Returns:
- a builder node that can be used to add more nodes to the XML document.
- Throws:
XMLBuilderRuntimeException
- to wrapParserConfigurationException
,SAXException
,IOException
,FileNotFoundException
-
stripWhitespaceOnlyTextNodes
public XMLBuilder2 stripWhitespaceOnlyTextNodes()
Description copied from class:BaseXMLBuilder
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. Uses approach I documented on StackOverflow: http://stackoverflow.com/a/979606/4970- Specified by:
stripWhitespaceOnlyTextNodes
in classBaseXMLBuilder
- Returns:
- a builder node at the same location as before the operation.
- Throws:
XMLBuilderRuntimeException
- to wrapXPathExpressionException
-
importXMLBuilder
public XMLBuilder2 importXMLBuilder(BaseXMLBuilder builder)
Description copied from class:BaseXMLBuilder
Imports another XMLBuilder document into this document at the current position. The entire document provided is imported.- Specified by:
importXMLBuilder
in classBaseXMLBuilder
- Parameters:
builder
- the XMLBuilder document to be imported.- Returns:
- a builder node at the same location as before the import, but now containing the entire document tree provided.
-
root
public XMLBuilder2 root()
- Specified by:
root
in classBaseXMLBuilder
- Returns:
- the builder node representing the root element of the XML document.
-
xpathFind
public XMLBuilder2 xpathFind(String xpath, NamespaceContext nsContext)
Description copied from class:BaseXMLBuilder
Find the first element in the builder's DOM matching the given XPath expression, where the expression may include namespaces if aNamespaceContext
is provided.- Specified by:
xpathFind
in classBaseXMLBuilder
- Parameters:
xpath
- 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.- Returns:
- a builder node representing the first Element that matches the XPath expression.
- Throws:
XMLBuilderRuntimeException
- to wrapXPathExpressionException
-
xpathFind
public XMLBuilder2 xpathFind(String xpath)
Description copied from class:BaseXMLBuilder
Find the first element in the builder's DOM matching the given XPath expression.- Specified by:
xpathFind
in classBaseXMLBuilder
- Parameters:
xpath
- An XPath expression that *must* resolve to an existing Element within the document object model.- Returns:
- a builder node representing the first Element that matches the XPath expression.
-
element
public XMLBuilder2 element(String name)
Description copied from class:BaseXMLBuilder
Add a named XML element to the document as a child of this builder node, and return the builder node representing the new child. When adding an element to a namespaced document, the new node will be assigned a namespace matching it's qualified name prefix (if any) or the document's default namespace. NOTE: If the element has a prefix that does not match any known namespaces, the element will be created without any namespace.- Specified by:
element
in classBaseXMLBuilder
- Parameters:
name
- the name of the XML element.- Returns:
- a builder node representing the new child.
-
elem
public XMLBuilder2 elem(String name)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.element(String)
.- Specified by:
elem
in classBaseXMLBuilder
- Parameters:
name
- the name of the XML element.- Returns:
- a builder node representing the new child.
-
e
public XMLBuilder2 e(String name)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.element(String)
.- Specified by:
e
in classBaseXMLBuilder
- Parameters:
name
- the name of the XML element.- Returns:
- a builder node representing the new child.
-
element
public XMLBuilder2 element(String name, String namespaceURI)
Description copied from class:BaseXMLBuilder
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.- Specified by:
element
in classBaseXMLBuilder
- Parameters:
name
- the name of the XML element.namespaceURI
- a namespace URI- Returns:
- a builder node representing the new child.
-
elementBefore
public XMLBuilder2 elementBefore(String name)
Description copied from class:BaseXMLBuilder
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. When adding an element to a namespaced document, the new node will be assigned a namespace matching it's qualified name prefix (if any) or the document's default namespace. NOTE: If the element has a prefix that does not match any known namespaces, the element will be created without any namespace.- Specified by:
elementBefore
in classBaseXMLBuilder
- Parameters:
name
- the name of the XML element.- Returns:
- a builder node representing the new child.
-
elementBefore
public XMLBuilder2 elementBefore(String name, String namespaceURI)
Description copied from class:BaseXMLBuilder
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.- Specified by:
elementBefore
in classBaseXMLBuilder
- Parameters:
name
- the name of the XML element.namespaceURI
- a namespace URI- Returns:
- a builder node representing the new child.
-
attribute
public XMLBuilder2 attribute(String name, String value)
Description copied from class:BaseXMLBuilder
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).- Specified by:
attribute
in classBaseXMLBuilder
- Parameters:
name
- the attribute's name.value
- the attribute's value.- Returns:
- the builder node representing the element to which the attribute was added.
-
attr
public XMLBuilder2 attr(String name, String value)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.attribute(String, String)
.- Specified by:
attr
in classBaseXMLBuilder
- Parameters:
name
- the attribute's name.value
- the attribute's value.- Returns:
- the builder node representing the element to which the attribute was added.
-
a
public XMLBuilder2 a(String name, String value)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.attribute(String, String)
.- Specified by:
a
in classBaseXMLBuilder
- Parameters:
name
- the attribute's name.value
- the attribute's value.- Returns:
- the builder node representing the element to which the attribute was added.
-
text
public XMLBuilder2 text(String value, boolean replaceText)
Description copied from class:BaseXMLBuilder
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).- Specified by:
text
in classBaseXMLBuilder
- Parameters:
value
- 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.- Returns:
- the builder node representing the element to which the text was added.
-
text
public XMLBuilder2 text(String value)
Description copied from class:BaseXMLBuilder
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).- Specified by:
text
in classBaseXMLBuilder
- Parameters:
value
- the text value to add to the element.- Returns:
- the builder node representing the element to which the text was added.
-
t
public XMLBuilder2 t(String value)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.text(String)
.- Specified by:
t
in classBaseXMLBuilder
- Parameters:
value
- the text value to add to the element.- Returns:
- the builder node representing the element to which the text was added.
-
cdata
public XMLBuilder2 cdata(String data)
Description copied from class:BaseXMLBuilder
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).- Specified by:
cdata
in classBaseXMLBuilder
- Parameters:
data
- the String value that will be added to a CDATA element.- Returns:
- the builder node representing the element to which the data was added.
-
data
public XMLBuilder2 data(String data)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.cdata(String)
.- Specified by:
data
in classBaseXMLBuilder
- Parameters:
data
- the String value that will be added to a CDATA element.- Returns:
- the builder node representing the element to which the data was added.
-
d
public XMLBuilder2 d(String data)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.cdata(String)
.- Specified by:
d
in classBaseXMLBuilder
- Parameters:
data
- the String value that will be added to a CDATA element.- Returns:
- the builder node representing the element to which the data was added.
-
cdata
public XMLBuilder2 cdata(byte[] data)
Description copied from class:BaseXMLBuilder
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).- Specified by:
cdata
in classBaseXMLBuilder
- Parameters:
data
- the data value that will be Base64-encoded and added to a CDATA element.- Returns:
- the builder node representing the element to which the data was added.
-
data
public XMLBuilder2 data(byte[] data)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.cdata(byte[])
.- Specified by:
data
in classBaseXMLBuilder
- Parameters:
data
- the data value that will be Base64-encoded and added to a CDATA element.- Returns:
- the builder node representing the element to which the data was added.
-
d
public XMLBuilder2 d(byte[] data)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.cdata(byte[])
.- Specified by:
d
in classBaseXMLBuilder
- Parameters:
data
- the data value that will be Base64-encoded and added to a CDATA element.- Returns:
- the builder node representing the element to which the data was added.
-
comment
public XMLBuilder2 comment(String comment)
Description copied from class:BaseXMLBuilder
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).- Specified by:
comment
in classBaseXMLBuilder
- Parameters:
comment
- the comment to add to the element.- Returns:
- the builder node representing the element to which the comment was added.
-
cmnt
public XMLBuilder2 cmnt(String comment)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.comment(String)
.- Specified by:
cmnt
in classBaseXMLBuilder
- Parameters:
comment
- the comment to add to the element.- Returns:
- the builder node representing the element to which the comment was added.
-
c
public XMLBuilder2 c(String comment)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.comment(String)
.- Specified by:
c
in classBaseXMLBuilder
- Parameters:
comment
- the comment to add to the element.- Returns:
- the builder node representing the element to which the comment was added.
-
instruction
public XMLBuilder2 instruction(String target, String data)
Description copied from class:BaseXMLBuilder
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).- Specified by:
instruction
in classBaseXMLBuilder
- Parameters:
target
- the target value for the instruction.data
- the data value for the instruction- Returns:
- the builder node representing the element to which the instruction was added.
-
inst
public XMLBuilder2 inst(String target, String data)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.instruction(String, String)
.- Specified by:
inst
in classBaseXMLBuilder
- Parameters:
target
- the target value for the instruction.data
- the data value for the instruction- Returns:
- the builder node representing the element to which the instruction was added.
-
i
public XMLBuilder2 i(String target, String data)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.instruction(String, String)
.- Specified by:
i
in classBaseXMLBuilder
- Parameters:
target
- the target value for the instruction.data
- the data value for the instruction- Returns:
- the builder node representing the element to which the instruction was added.
-
insertInstruction
public XMLBuilder2 insertInstruction(String target, String data)
Description copied from class:BaseXMLBuilder
Insert an instruction before the element represented by this builder node, and return the node representing that same element (not the new instruction node).- Specified by:
insertInstruction
in classBaseXMLBuilder
- Parameters:
target
- the target value for the instruction.data
- the data value for the instruction- Returns:
- the builder node representing the element before which the instruction was inserted.
-
reference
public XMLBuilder2 reference(String name)
Description copied from class:BaseXMLBuilder
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).- Specified by:
reference
in classBaseXMLBuilder
- Parameters:
name
- the name value for the reference.- Returns:
- the builder node representing the element to which the reference was added.
-
ref
public XMLBuilder2 ref(String name)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.reference(String)
.- Specified by:
ref
in classBaseXMLBuilder
- Parameters:
name
- the name value for the reference.- Returns:
- the builder node representing the element to which the reference was added.
-
r
public XMLBuilder2 r(String name)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.reference(String)
.- Specified by:
r
in classBaseXMLBuilder
- Parameters:
name
- the name value for the reference.- Returns:
- the builder node representing the element to which the reference was added.
-
namespace
public XMLBuilder2 namespace(String prefix, String namespaceURI)
Description copied from class:BaseXMLBuilder
Add an XML namespace attribute to this builder's element node.- Specified by:
namespace
in classBaseXMLBuilder
- Parameters:
prefix
- 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 uri- Returns:
- the builder node representing the element to which the attribute was added.
-
ns
public XMLBuilder2 ns(String prefix, String namespaceURI)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.namespace(String, String)
.- Specified by:
ns
in classBaseXMLBuilder
- Parameters:
prefix
- 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 uri- Returns:
- the builder node representing the element to which the attribute was added.
-
namespace
public XMLBuilder2 namespace(String namespaceURI)
Description copied from class:BaseXMLBuilder
Add an XML namespace attribute to this builder's element node without a prefix.- Specified by:
namespace
in classBaseXMLBuilder
- Parameters:
namespaceURI
- a namespace uri- Returns:
- the builder node representing the element to which the attribute was added.
-
ns
public XMLBuilder2 ns(String namespaceURI)
Description copied from class:BaseXMLBuilder
Synonym forBaseXMLBuilder.namespace(String)
.- Specified by:
ns
in classBaseXMLBuilder
- Parameters:
namespaceURI
- a namespace uri- Returns:
- the builder node representing the element to which the attribute was added.
-
up
public XMLBuilder2 up(int steps)
Description copied from class:BaseXMLBuilder
Return the builder node representing the nth ancestor element of this node, or the root node if n exceeds the document's depth.- Specified by:
up
in classBaseXMLBuilder
- Parameters:
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.- Returns:
- the nth ancestor of this node, or the root node if this is reached before the nth parent is found.
-
up
public XMLBuilder2 up()
Description copied from class:BaseXMLBuilder
Return the builder node representing the parent of the current node.- Specified by:
up
in classBaseXMLBuilder
- Returns:
- the parent of this node, or the root node if this method is called on the root node.
-
document
public XMLBuilder2 document()
Description copied from class:BaseXMLBuilder
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.- Specified by:
document
in classBaseXMLBuilder
- Returns:
- the builder node representing the root XML document.
-
asString
public String asString()
Description copied from class:BaseXMLBuilder
Serialize the XML document to a string excluding the XML declaration.- Overrides:
asString
in classBaseXMLBuilder
- Returns:
- the XML document as a string without the XML declaration at the beginning of the output.
- Throws:
XMLBuilderRuntimeException
- to wrapTransformerException
-
asString
public String asString(Properties properties)
Description copied from class:BaseXMLBuilder
Serialize the XML document to a string by delegating to theBaseXMLBuilder.toWriter(Writer, Properties)
method. If output options are provided, these options are provided to theTransformer
serializer.- Overrides:
asString
in classBaseXMLBuilder
- Parameters:
properties
- settings for theTransformer
serializer. This parameter may be null or an empty Properties object, in which case the default output properties will be applied.- Returns:
- the XML document as a string
- Throws:
XMLBuilderRuntimeException
- to wrapTransformerException
-
elementAsString
public String elementAsString()
Description copied from class:BaseXMLBuilder
Serialize the current XML Element and its descendants to a string excluding the XML declaration.- Overrides:
elementAsString
in classBaseXMLBuilder
- Returns:
- the XML document as a string without the XML declaration at the beginning of the output.
- Throws:
XMLBuilderRuntimeException
- to wrapTransformerException
-
elementAsString
public String elementAsString(Properties outputProperties)
Description copied from class:BaseXMLBuilder
Serialize the current XML Element and its descendants to a string by delegating to theBaseXMLBuilder.toWriter(Writer, Properties)
method. If output options are provided, these options are provided to theTransformer
serializer.- Overrides:
elementAsString
in classBaseXMLBuilder
- Parameters:
outputProperties
- settings for theTransformer
serializer. This parameter may be null or an empty Properties object, in which case the default output properties will be applied.- Returns:
- the XML document as a string
- Throws:
XMLBuilderRuntimeException
- to wrapTransformerException
-
toWriter
public void toWriter(boolean wholeDocument, Writer writer, Properties outputProperties)
Description copied from class:BaseXMLBuilder
Serialize either the specific Element wrapped by this BaseXMLBuilder, or its entire XML document, to the given writer using the defaultTransformerFactory
andTransformer
classes. If output options are provided, these options are provided to theTransformer
serializer.- Overrides:
toWriter
in classBaseXMLBuilder
- Parameters:
wholeDocument
- if true the whole XML document (i.e. the document root) is serialized, if false just the current Element and its descendants are serialized.writer
- a writer to which the serialized document is written.outputProperties
- settings for theTransformer
serializer. This parameter may be null or an empty Properties object, in which case the default output properties will be applied.- Throws:
XMLBuilderRuntimeException
- to wrapTransformerException
-
toWriter
public void toWriter(Writer writer, Properties outputProperties)
Description copied from class:BaseXMLBuilder
Serialize the XML document to the given writer using the defaultTransformerFactory
andTransformer
classes. If output options are provided, these options are provided to theTransformer
serializer.- Overrides:
toWriter
in classBaseXMLBuilder
- Parameters:
writer
- a writer to which the serialized document is written.outputProperties
- settings for theTransformer
serializer. This parameter may be null or an empty Properties object, in which case the default output properties will be applied.- Throws:
XMLBuilderRuntimeException
- to wrapTransformerException
-
xpathQuery
public Object xpathQuery(String xpath, QName type, NamespaceContext nsContext)
Description copied from class:BaseXMLBuilder
Return the result of evaluating an XPath query on the builder's DOM using the given namespace. Returns null if the query finds nothing, or finds a node that does not match the type specified by returnType.- Overrides:
xpathQuery
in classBaseXMLBuilder
- Parameters:
xpath
- an XPath expressiontype
- the type the XPath is expected to resolve to, e.g:XPathConstants.NODE
,XPathConstants.NODESET
,XPathConstants.STRING
.nsContext
- a mapping of prefixes to namespace URIs that allows the XPath expression to use namespaces, or null for a non-namespaced document.- Returns:
- a builder node representing the first Element that matches the XPath expression.
- Throws:
XMLBuilderRuntimeException
- to wrapXPathExpressionException
-
xpathQuery
public Object xpathQuery(String xpath, QName type)
Description copied from class:BaseXMLBuilder
Return the result of evaluating an XPath query on the builder's DOM. Returns null if the query finds nothing, or finds a node that does not match the type specified by returnType.- Overrides:
xpathQuery
in classBaseXMLBuilder
- Parameters:
xpath
- an XPath expressiontype
- the type the XPath is expected to resolve to, e.g:XPathConstants.NODE
,XPathConstants.NODESET
,XPathConstants.STRING
- Returns:
- a builder node representing the first Element that matches the XPath expression.
- Throws:
XMLBuilderRuntimeException
- to wrapXPathExpressionException
-
-