public interface XMLTag
XMLTag
is not thread-safe and cannot be considered so, even for non mutating methods.
This is due to the current location which needs to be kept, as we work in a fluent interface style.Modifier and Type | Method and Description |
---|---|
XMLTag |
addAttribute(Attr attr)
Add given attribute to current element
|
XMLTag |
addAttribute(String name,
String value)
Create a new attribute for the current node
|
XMLTag |
addCDATA(CDATASection data)
Add a CDATA note to the current tag
|
XMLTag |
addCDATA(String data)
Add a data node under the current node, and jump to the parent node.
|
XMLTag |
addDocument(Document doc)
Inserts another
Document instance under the current tag |
XMLTag |
addDocument(XMLTag tag)
Inserts another
XMLTag instance under the current tag. |
XMLTag |
addNamespace(String prefix,
String namespaceURI)
Add a namespace to the document
|
XMLTag |
addTag(Element tag)
Inserts a
Element instance and its hierarchy under the current tag |
XMLTag |
addTag(String name)
Create a tag under the current location and use it as the current node
|
XMLTag |
addTag(XMLTag tag)
Inserts another
XMLTag tag hierarchy under the current tag. |
XMLTag |
addText(String text)
Add a text node under the current node, and jump to the parent node.
|
XMLTag |
addText(Text text)
Add a text note to the current tag
|
XMLTag |
delete()
Delete current tag and its childs.
|
XMLTag |
deleteAttribute(String name)
Delete an attribute of the current node.
|
XMLTag |
deleteAttributeIfExists(String name)
Delete an attribute of the current node, if it exists
|
XMLTag |
deleteAttributes()
Delete all existing attributes of current node
|
XMLTag |
deleteChilds()
Delete all existing elements of this node
|
XMLTag |
deletePrefixes()
Remove any prefix and namespaces contained in the tag name, childs and attributes, thus changing namespace and tag name.
|
XMLTag |
duplicate() |
String |
findAttribute(String name)
returns the attribute value of the current node or null if the attribute does not exist
|
String |
findAttribute(String name,
String relativeXpath,
Object... arguments)
Returns the attribute value of the node pointed by given XPath expression or null if the attribute does not exist
|
XMLTag |
forEach(CallBack callBack,
String relativeXpath,
Object... arguments)
Execute an action for each selected tags from the current node.
|
XMLTag |
forEach(String xpath,
CallBack callBack) |
XMLTag |
forEachChild(CallBack callBack)
Execute an action for each child in the current node.
|
String |
getAttribute(String name)
returns the attribute value of the current node
|
String |
getAttribute(String name,
String relativeXpath,
Object... arguments)
Returns the attribute value of the node pointed by given XPath expression
|
String[] |
getAttributeNames() |
String |
getCDATA() |
String |
getCDATA(String relativeXpath,
Object... arguments)
Get the CDATA of a selected node
|
String |
getCDATAorText() |
String |
getCDATAorText(String relativeXpath,
Object... arguments)
Get the text of a sepcific node
|
int |
getChildCount() |
List<Element> |
getChildElement() |
Iterable<XMLTag> |
getChilds()
XMLTag tag = XMLDoc.newDocument(true)
.addRoot("root").addTag("a")
.gotoParent().addTag("b")
.gotoParent().addTag("c")
.gotoRoot();
assertEquals(tag.getCurrentTagName(), "root");
for (XMLTag xmlTag : tag.getChilds()) {
if(xmlTag.getCurrentTagName().equals("b")) {
break;
}
}
assertEquals(tag.getCurrentTagName(), "b"); |
Iterable<XMLTag> |
getChilds(String relativeXpath,
Object... arguments)
Create an iterable object over selected elements.
|
NamespaceContext |
getContext() |
Element |
getCurrentTag() |
String |
getCurrentTagLocation() |
String |
getCurrentTagName() |
XMLTag |
getInnerDocument() |
String |
getInnerText() |
String |
getPefix(String namespaceURI)
Get the prefix of a namespace
|
String[] |
getPefixes(String namespaceURI)
Get all bound prefixes of a namespace
|
String |
getText() |
String |
getText(String relativeXpath,
Object... arguments)
Get the text of a sepcific node
|
String |
getTextOrCDATA() |
String |
getTextOrCDATA(String relativeXpath,
Object... arguments)
Get the text of a sepcific node
|
XMLTag |
gotoChild()
Go to the only child element of the curent node.
|
XMLTag |
gotoChild(int i)
Go to the Nth child of the curent node.
|
XMLTag |
gotoChild(String nodeName)
Go to the child found with given node name
|
XMLTag |
gotoFirstChild()
Go to the first child element of the curent node.
|
XMLTag |
gotoFirstChild(String name)
Go to the first child occurance found having given name
|
XMLTag |
gotoLastChild()
Go to the lastest child element of the curent node.
|
XMLTag |
gotoLastChild(String name)
Go to the last child occurance found having given name
|
XMLTag |
gotoParent()
Go to parent tag.
|
XMLTag |
gotoRoot()
Go to document root tag
|
XMLTag |
gotoTag(String relativeXpath,
Object... arguments)
Go to a specific node
|
boolean |
hasAttribute(String name)
Check wheter current tag contains an atribute
|
boolean |
hasAttribute(String name,
String relativeXpath,
Object... arguments)
Check if targeted tag has an attribute of given name
|
boolean |
hasTag(String relativeXpath,
Object... arguments)
Check if a tag exist in the document
|
Boolean |
rawXpathBoolean(String relativeXpath,
Object... arguments)
Execute an XPath expression directly using the Java XPath API, from the current node.
|
Node |
rawXpathNode(String relativeXpath,
Object... arguments)
Execute an XPath expression directly using the Java XPath API, from the current node.
|
NodeList |
rawXpathNodeSet(String relativeXpath,
Object... arguments)
Execute an XPath expression directly using the Java XPath API, from the current node.
|
Number |
rawXpathNumber(String relativeXpath,
Object... arguments)
Execute an XPath expression directly using the Java XPath API, from the current node.
|
String |
rawXpathString(String relativeXpath,
Object... arguments)
Execute an XPath expression directly using the Java XPath API, from the current node.
|
XMLTag |
renameTo(String newTagName)
Replace current element name by another name
|
XMLTag |
setAttribute(String name,
String value)
Sets the new value on an existign attribute, and remains on the current tag.
|
XMLTag |
setAttribute(String name,
String value,
String relativeXpath,
Object... arguments)
Sets the new value on a targetted node's attribute, and remains on the current tag.
|
XMLTag |
setAttributeIfExist(String name,
String value)
Sets the new value on an attribute, and remains on the current tag.
|
XMLTag |
setAttributeIfExist(String name,
String value,
String relativeXpath,
Object... arguments)
Sets the new value on a targetted node's attribute, and remains on the current tag.
|
XMLTag |
setCDATA(String data)
Set the cdata in the current node.
|
XMLTag |
setCDATA(String data,
String relativeXpath,
Object... arguments)
Set the cdata in the targetted node.
|
XMLTag |
setCDATAIfExist(String data,
String relativeXpath,
Object... arguments)
Set the cdata in the targetted node.
|
XMLTag |
setText(String text)
Set the text in the current node.
|
XMLTag |
setText(String text,
String relativeXpath,
Object... arguments)
Set the text in the targetted node.
|
XMLTag |
setTextIfExist(String text,
String relativeXpath,
Object... arguments)
Set the text in the targetted node.
|
byte[] |
toBytes() |
byte[] |
toBytes(String encoding) |
Document |
toDocument() |
OutputStream |
toOutputStream() |
OutputStream |
toOutputStream(String encoding) |
Result |
toResult() |
XMLTag |
toResult(Result out)
Converts this document to the result provided
|
XMLTag |
toResult(Result out,
String encoding)
Converts this document to the result provided, overriding default encoding of xml document
|
Result |
toResult(String encoding) |
Source |
toSource() |
XMLTag |
toStream(OutputStream out)
Write this document to a stream
|
XMLTag |
toStream(OutputStream out,
String encoding)
Write this document to a stream
|
XMLTag |
toStream(Writer out)
Write this document to a stream
|
XMLTag |
toStream(Writer out,
String encoding)
Write this document to a stream
|
String |
toString() |
String |
toString(String encoding) |
Writer |
toWriter() |
Writer |
toWriter(String encoding) |
ValidationResult |
validate(Source... schemas)
Validate this document against specifief schemas
|
ValidationResult |
validate(URL... schemaLocations)
Validate this document against specifief schemas
|
XMLTag addNamespace(String prefix, String namespaceURI)
prefix
- The prefix of the namespacenamespaceURI
- The URI of the namespaceXMLTag addTag(String name)
name
- Name of the element to addXMLTag addAttribute(String name, String value)
name
- Name of the attribute to addvalue
- value of the attribute to addXMLTag addText(String text)
addTag("name").addText("Bob")addTag("sex").addText("M")addTag("age").addText("30")
<name>Bob</name><sex>M</sex><age>30</age>
text
- the text to addXMLTag addCDATA(String data)
addTag("name").addCDATA("Bob")addTag("sex").addCDATA("M")addTag("age").addCDATA("30")
<name><![CDATA[Bob]]></name><sex><![CDATA[M]]></sex><age><![CDATA[30]]></age>
data
- the data to addXMLTag addDocument(XMLTag tag)
XMLTag
instance under the current tag. The whole document will be inserted.tag
- The XMLTag
instance to insertXMLTag addDocument(Document doc)
Document
instance under the current tagdoc
- The Document
instance to insertXMLTag addTag(XMLTag tag)
XMLTag
tag hierarchy under the current tag. Only the current tag of the given document
will be inserted with its hierarchy, not the whole document.tag
- The XMLTag
current tag hierarchy to insertXMLTag addTag(Element tag)
Element
instance and its hierarchy under the current tagtag
- The Element
instance to insertXMLTag addAttribute(Attr attr)
attr
- The attribute to insertXMLTag addText(Text text)
text
- The node to insertXMLTag addCDATA(CDATASection data)
data
- The node to insertXMLTag gotoParent()
XMLTag gotoRoot()
XMLTag gotoTag(String relativeXpath, Object... arguments) throws XMLDocumentException
relativeXpath
- XPath expresionarguments
- to be replaced in xpath expression before compiling. Uses String.format() to build XPath expression.XMLDocumentException
- if the node does not exist or if the XPath expression is invalidXMLTag gotoChild() throws XMLDocumentException
XMLDocumentException
- If the current node has several childs or no child at allXMLTag gotoChild(int i) throws XMLDocumentException
i
- index of the child, from 1 to child element numberXMLDocumentException
- If the child node does not existXMLTag gotoChild(String nodeName) throws XMLDocumentException
nodeName
- name of the child to find.XMLDocumentException
- If the element with this name has not been found or if there are too many elementsXMLTag gotoFirstChild() throws XMLDocumentException
XMLDocumentException
- If the current node has no child at allXMLTag gotoFirstChild(String name) throws XMLDocumentException
name
- Name of the child to go atXMLDocumentException
- If the current node has no child at allXMLTag gotoLastChild() throws XMLDocumentException
XMLDocumentException
- If the current node has no child at allXMLTag gotoLastChild(String name) throws XMLDocumentException
name
- Name of the child to go atXMLDocumentException
- If the current node has no child at allboolean hasTag(String relativeXpath, Object... arguments)
relativeXpath
- XPath expression where the tag should be locatedarguments
- XPath arguments. Uses String.format() to build XPath expression.boolean hasAttribute(String name, String relativeXpath, Object... arguments) throws XMLDocumentException
name
- the name of the attributerelativeXpath
- XPath that target the tagarguments
- optional arguments of xpath expression. Uses String.format() to build XPath expression.XMLDocumentException
- If the targetted node does not exist or if xpath expression is not validboolean hasAttribute(String name)
name
- Attribute nameXMLTag forEachChild(CallBack callBack)
callBack
- Callback method to run after the current tag of the document has changed to a childXMLTag forEach(CallBack callBack, String relativeXpath, Object... arguments)
callBack
- Callback method to run after the current tag of the document has changed to a childrelativeXpath
- XXath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.String rawXpathString(String relativeXpath, Object... arguments)
relativeXpath
- The XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XPathConstants.STRING
return typeNumber rawXpathNumber(String relativeXpath, Object... arguments)
relativeXpath
- The XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XPathConstants.NUMBER
return typeBoolean rawXpathBoolean(String relativeXpath, Object... arguments)
relativeXpath
- The XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XPathConstants.BOOLEAN
return typeNode rawXpathNode(String relativeXpath, Object... arguments)
relativeXpath
- The XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XPathConstants.NODE
return typeNodeList rawXpathNodeSet(String relativeXpath, Object... arguments)
relativeXpath
- The XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XPathConstants.NODESET
return typeElement getCurrentTag()
int getChildCount()
Iterable<XMLTag> getChilds()
XMLTag tag = XMLDoc.newDocument(true)
.addRoot("root").addTag("a")
.gotoParent().addTag("b")
.gotoParent().addTag("c")
.gotoRoot();
assertEquals(tag.getCurrentTagName(), "root");
for (XMLTag xmlTag : tag.getChilds()) {
if(xmlTag.getCurrentTagName().equals("b")) {
break;
}
}
assertEquals(tag.getCurrentTagName(), "b");
XMLTag tag = XMLDoc.newDocument(true)
.addRoot("root").addTag("a")
.gotoParent().addTag("b")
.gotoParent().addTag("c")
.gotoRoot();
assertEquals(tag.getCurrentTagName(), "root");
for (XMLTag xmlTag : tag.getChilds()) {
System.out.println(xmlTag.getCurrentTagName());
}
assertEquals(tag.getCurrentTagName(), "root");
Iterable<XMLTag> getChilds(String relativeXpath, Object... arguments)
relativeXpath
- XPath to select tagsarguments
- XPath argumentsString getCurrentTagName()
String getCurrentTagLocation()
NamespaceContext getContext()
String getPefix(String namespaceURI)
namespaceURI
- The URI of the namespacejavadoc
)String[] getPefixes(String namespaceURI)
namespaceURI
- The URI of the namespaceString getText()
String getText(String relativeXpath, Object... arguments) throws XMLDocumentException
relativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is not valid or if the node does not existString getCDATA()
String getCDATA(String relativeXpath, Object... arguments) throws XMLDocumentException
relativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is invalid or if the node does not existString getTextOrCDATA()
String getTextOrCDATA(String relativeXpath, Object... arguments) throws XMLDocumentException
relativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is not valid or if the node does not existString getCDATAorText()
String getCDATAorText(String relativeXpath, Object... arguments) throws XMLDocumentException
relativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is not valid or if the node does not existString[] getAttributeNames()
String getAttribute(String name) throws XMLDocumentException
name
- attribute nameXMLDocumentException
- If the attribute does not existString getAttribute(String name, String relativeXpath, Object... arguments) throws XMLDocumentException
name
- attribute namerelativeXpath
- XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- Attribute does not exist, targetted node or XPath expression is invalidString findAttribute(String name)
name
- attribute nameString findAttribute(String name, String relativeXpath, Object... arguments) throws XMLDocumentException
name
- attribute namerelativeXpath
- XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- targetted node does not exist or XPath expression is invalidXMLTag getInnerDocument()
String getInnerText()
XMLTag duplicate()
XMLTag setText(String text)
gotoChild("name").setText("Bob").gotoChild("sex").setText("M").gotoChild("age").setText("30")
<name>Bob</name><sex>M</sex><age>30</age>
text
- text to put under this nodeXMLTag setText(String text, String relativeXpath, Object... arguments) throws XMLDocumentException
text
- text to put under this noderelativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is invalid or if the node does not existXMLTag setTextIfExist(String text, String relativeXpath, Object... arguments) throws XMLDocumentException
text
- text to put under this noderelativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is invalidXMLTag setCDATA(String data)
gotoChild("name").setText("Bob").gotoChild("sex").setText("M").gotoChild("age").setText("30")
<name>Bob</name><sex>M</sex><age>30</age>
data
- text to put under this node in a cdata sectionXMLTag setCDATA(String data, String relativeXpath, Object... arguments) throws XMLDocumentException
data
- text to put under this node in a cdata sectionrelativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is invalid or if the node does not existXMLTag setCDATAIfExist(String data, String relativeXpath, Object... arguments) throws XMLDocumentException
data
- text to put under this node in a cdata sectionrelativeXpath
- XPath expression that select the nodearguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- If the XPath expression is invalidXMLTag setAttribute(String name, String value) throws XMLDocumentException
name
- attribute namevalue
- new attribute'svalueXMLDocumentException
- If the attribute does not existXMLTag setAttributeIfExist(String name, String value)
name
- attribute namevalue
- new attribute valueXMLTag setAttribute(String name, String value, String relativeXpath, Object... arguments) throws XMLDocumentException
name
- attribute namevalue
- new attribute's valuerelativeXpath
- XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- Attribute does not exist, targetted node does not exit, or XPath expression is invalidXMLTag setAttributeIfExist(String name, String value, String relativeXpath, Object... arguments) throws XMLDocumentException
name
- attribute namevalue
- new attribute's valuerelativeXpath
- XPath expressionarguments
- facultative Xpath expression arguments. Uses String.format() to build XPath expression.XMLDocumentException
- XPath expression is invalid or targetted node does not existXMLTag deleteChilds()
XMLTag delete() throws XMLDocumentException
XMLDocumentException
- if the current node is the root nodeXMLTag deleteAttributes()
XMLTag deleteAttribute(String name) throws XMLDocumentException
name
- attribute nameXMLDocumentException
- if the attribute does not existXMLTag deleteAttributeIfExists(String name)
name
- attribute nameXMLTag renameTo(String newTagName)
newTagName
- New name of the tagXMLTag deletePrefixes()
Document toDocument()
String toString()
String toString(String encoding)
encoding
- destination encoding of XML documentbyte[] toBytes()
byte[] toBytes(String encoding)
encoding
- The encoding to useXMLTag toResult(Result out)
out
- The output resultXMLTag toResult(Result out, String encoding)
out
- The output resultencoding
- The new encodingXMLTag toStream(OutputStream out)
out
- The output resultXMLTag toStream(OutputStream out, String encoding)
out
- The output resultencoding
- The new encodingXMLTag toStream(Writer out)
out
- The output resultXMLTag toStream(Writer out, String encoding)
out
- The output resultencoding
- The new encodingResult toResult()
Result
representation of this document. Useful when using web services for example.Result toResult(String encoding)
encoding
- The new encodingResult
representation of this document. Useful when using web services for example.OutputStream toOutputStream()
OutputStream toOutputStream(String encoding)
encoding
- The new encodingWriter toWriter()
Writer toWriter(String encoding)
encoding
- The new encodingValidationResult validate(Source... schemas)
schemas
- A list of schemasValidationResult validate(URL... schemaLocations)
schemaLocations
- A list of schemasCopyright © 2008–2020 Mathieu Carbou. All rights reserved.