Class XMLDoc
java.lang.Object
com.mycila.xmltool.XMLDoc
- All Implemented Interfaces:
XMLTag
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAttribute(String name, String value) Create a new attribute for the current nodeaddAttribute(Attr attr) Add given attribute to current elementAdd a data node under the current node, and jump to the parent node.addCDATA(CDATASection data) Add a CDATA note to the current tagaddDocument(XMLTag tag) Inserts anotherXMLTaginstance under the current tag.addDocument(Document doc) Inserts anotherDocumentinstance under the current tagaddNamespace(String prefix, String namespaceURI) Add a namespace to the documentInserts anotherXMLTagtag hierarchy under the current tag.Create a tag under the current location and use it as the current nodeInserts aElementinstance and its hierarchy under the current tagAdd a text node under the current node, and jump to the parent node.Add a text note to the current tagchilds(short type) delete()Delete current tag and its childs.deleteAttribute(String name) Delete an attribute of the current node.Delete an attribute of the current node, if it existsDelete all existing attributes of current nodeDelete all existing elements of this nodeRemove any prefix and namespaces contained in the tag name, childs and attributes, thus changing namespace and tag name.findAttribute(String name) returns the attribute value of the current node or null if the attribute does not existfindAttribute(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 existExecute an action for each selected tags from the current node.forEachChild(CallBack callBack) Execute an action for each child in the current node.static XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagfrom(InputStream is) static XMLTagfrom(InputStream is, boolean ignoreNamespaces) static XMLTagfrom(InputStream is, boolean ignoreNamespaces, String encoding) static XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagstatic XMLTagfrom(InputSource source) static XMLTagfrom(InputSource source, boolean ignoreNamespaces) static XMLTagfromCurrentTag(XMLTag tag, boolean ignoreNamespaces) Create anotherXMLTaginstance from the hierarchy under the current tag.getAttribute(String name) returns the attribute value of the current nodegetAttribute(String name, String relativeXpath, Object... arguments) Returns the attribute value of the node pointed by given XPath expressionString[]getCDATA()Get the CDATA of a selected nodegetCDATAorText(String relativeXpath, Object... arguments) Get the text of a sepcific nodeintXMLTag 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");Create an iterable object over selected elements.Get the prefix of a namespaceString[]getPefixes(String namespaceURI) Get all bound prefixes of a namespacegetText()Get the text of a sepcific nodegetTextOrCDATA(String relativeXpath, Object... arguments) Get the text of a sepcific nodeGo to the only child element of the curent node.gotoChild(int i) Go to the Nth child of the curent node.Go to the child found with given node nameGo to the first child element of the curent node.gotoFirstChild(String name) Go to the first child occurance found having given nameGo to the lastest child element of the curent node.gotoLastChild(String name) Go to the last child occurance found having given nameGo to parent tag.gotoRoot()Go to document root tagGo to a specific nodebooleanhasAttribute(String name) Check wheter current tag contains an atributebooleanhasAttribute(String name, String relativeXpath, Object... arguments) Check if targeted tag has an attribute of given namebooleanCheck if a tag exist in the documentprivate booleanstatic XMLDocBuilderstatic XMLDocBuildernewDocument(boolean ignoreNamespaces) rawXpathBoolean(String relativeXpath, Object... arguments) Execute an XPath expression directly using the Java XPath API, from the current node.rawXpathNode(String relativeXpath, Object... arguments) Execute an XPath expression directly using the Java XPath API, from the current node.rawXpathNodeSet(String relativeXpath, Object... arguments) Execute an XPath expression directly using the Java XPath API, from the current node.rawXpathNumber(String relativeXpath, Object... arguments) Execute an XPath expression directly using the Java XPath API, from the current node.rawXpathString(String relativeXpath, Object... arguments) Execute an XPath expression directly using the Java XPath API, from the current node.Replace current element name by another namesetAttribute(String name, String value) Sets the new value on an existign attribute, and remains on the current tag.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.setAttributeIfExist(String name, String value) Sets the new value on an attribute, and remains on the current tag.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.Set the cdata in the current node.Set the cdata in the targetted node.setCDATAIfExist(String data, String relativeXpath, Object... arguments) Set the cdata in the targetted node.Set the text in the current node.Set the text in the targetted node.setTextIfExist(String text, String relativeXpath, Object... arguments) Set the text in the targetted node.byte[]toBytes()byte[]toOutputStream(String encoding) toResult()Converts this document to the result providedConverts this document to the result provided, overriding default encoding of xml documenttoSource()toStream(OutputStream out) Write this document to a streamtoStream(OutputStream out, String encoding) Write this document to a streamWrite this document to a streamWrite this document to a streamtoString()toWriter()Validate this document against specifief schemasValidate this document against specifief schemas
-
Field Details
-
definition
-
current
Element current
-
-
Constructor Details
-
XMLDoc
XMLDoc(XMLDocDefinition definition)
-
-
Method Details
-
getContext
- Specified by:
getContextin interfaceXMLTag- Returns:
- the namespace context
-
hasAttribute
Description copied from interface:XMLTagCheck wheter current tag contains an atribute- Specified by:
hasAttributein interfaceXMLTag- Parameters:
name- Attribute name- Returns:
- true if the attribute is in current tag
-
hasAttribute
Description copied from interface:XMLTagCheck if targeted tag has an attribute of given name- Specified by:
hasAttributein interfaceXMLTag- Parameters:
name- the name of the attributerelativeXpath- XPath that target the tagarguments- optional arguments of xpath expression. Uses String.format() to build XPath expression.- Returns:
- true if the tag exist with this attribute name
-
hasTag
Description copied from interface:XMLTagCheck if a tag exist in the document -
forEachChild
Description copied from interface:XMLTagExecute an action for each child in the current node.- Specified by:
forEachChildin interfaceXMLTag- Parameters:
callBack- Callback method to run after the current tag of the document has changed to a child- Returns:
- this
-
forEach
Description copied from interface:XMLTagExecute an action for each selected tags from the current node.- Specified by:
forEachin interfaceXMLTag- Parameters:
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.- Returns:
- this
-
forEach
-
rawXpathString
Description copied from interface:XMLTagExecute an XPath expression directly using the Java XPath API, from the current node.- Specified by:
rawXpathStringin interfaceXMLTag- Parameters:
relativeXpath- The XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The
XPathConstants.STRINGreturn type
-
rawXpathNumber
Description copied from interface:XMLTagExecute an XPath expression directly using the Java XPath API, from the current node.- Specified by:
rawXpathNumberin interfaceXMLTag- Parameters:
relativeXpath- The XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The
XPathConstants.NUMBERreturn type
-
rawXpathBoolean
Description copied from interface:XMLTagExecute an XPath expression directly using the Java XPath API, from the current node.- Specified by:
rawXpathBooleanin interfaceXMLTag- Parameters:
relativeXpath- The XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The
XPathConstants.BOOLEANreturn type
-
rawXpathNode
Description copied from interface:XMLTagExecute an XPath expression directly using the Java XPath API, from the current node.- Specified by:
rawXpathNodein interfaceXMLTag- Parameters:
relativeXpath- The XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The
XPathConstants.NODEreturn type
-
rawXpathNodeSet
Description copied from interface:XMLTagExecute an XPath expression directly using the Java XPath API, from the current node.- Specified by:
rawXpathNodeSetin interfaceXMLTag- Parameters:
relativeXpath- The XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The
XPathConstants.NODESETreturn type
-
getPefix
-
getPefixes
Description copied from interface:XMLTagGet all bound prefixes of a namespace- Specified by:
getPefixesin interfaceXMLTag- Parameters:
namespaceURI- The URI of the namespace- Returns:
- a list of prefixes or an empty array.
-
addNamespace
-
addDocument
-
addDocument
-
addTag
-
addTag
-
addTag
-
addAttribute
-
addAttribute
Description copied from interface:XMLTagAdd given attribute to current element- Specified by:
addAttributein interfaceXMLTag- Parameters:
attr- The attribute to insert- Returns:
- this
-
addText
Description copied from interface:XMLTagAdd a text node under the current node, and jump to the parent node. This enables the create or quick documents like this:addTag("name").addText("Bob")addTag("sex").addText("M")addTag("age").addText("30")<name>Bob</name><sex>M</sex><age>30</age> -
addText
-
addCDATA
Description copied from interface:XMLTagAdd a data node under the current node, and jump to the parent node. This enables the create or quick documents like this:addTag("name").addCDATA("Bob")addTag("sex").addCDATA("M")addTag("age").addCDATA("30")<name><![CDATA[Bob]]></name><sex><![CDATA[M]]></sex><age><![CDATA[30]]></age> -
addCDATA
Description copied from interface:XMLTagAdd a CDATA note to the current tag -
delete
-
deleteChilds
Description copied from interface:XMLTagDelete all existing elements of this node- Specified by:
deleteChildsin interfaceXMLTag- Returns:
- this
-
deleteAttributes
Description copied from interface:XMLTagDelete all existing attributes of current node- Specified by:
deleteAttributesin interfaceXMLTag- Returns:
- this
-
deleteAttribute
Description copied from interface:XMLTagDelete an attribute of the current node.- Specified by:
deleteAttributein interfaceXMLTag- Parameters:
name- attribute name- Returns:
- this
-
deleteAttributeIfExists
Description copied from interface:XMLTagDelete an attribute of the current node, if it exists- Specified by:
deleteAttributeIfExistsin interfaceXMLTag- Parameters:
name- attribute name- Returns:
- this
-
renameTo
-
deletePrefixes
Description copied from interface:XMLTagRemove any prefix and namespaces contained in the tag name, childs and attributes, thus changing namespace and tag name. This can be very useful if you are working in a document when you want to ignore namespaces, if you don't know the tag prefix- Specified by:
deletePrefixesin interfaceXMLTag- Returns:
- this
-
getInnerDocument
- Specified by:
getInnerDocumentin interfaceXMLTag- Returns:
- Another XMLTag instance in which the current tag becomes de root element of the new document, and it contains all inner elements as in the previous document.
-
getInnerText
- Specified by:
getInnerTextin interfaceXMLTag- Returns:
- The text representation of the inner nodes of the current node. The current node is outputed as the root element of inner tags.
-
gotoParent
Description copied from interface:XMLTagGo to parent tag. Do nothing if we are already at root- Specified by:
gotoParentin interfaceXMLTag- Returns:
- this
-
gotoRoot
-
gotoChild
-
gotoChild
-
gotoChild
-
gotoFirstChild
Description copied from interface:XMLTagGo to the first child element of the curent node.- Specified by:
gotoFirstChildin interfaceXMLTag- Returns:
- this
- Throws:
XMLDocumentException- If the current node has no child at all
-
gotoFirstChild
Description copied from interface:XMLTagGo to the first child occurance found having given name- Specified by:
gotoFirstChildin interfaceXMLTag- Parameters:
name- Name of the child to go at- Returns:
- this
- Throws:
XMLDocumentException- If the current node has no child at all
-
gotoLastChild
Description copied from interface:XMLTagGo to the lastest child element of the curent node.- Specified by:
gotoLastChildin interfaceXMLTag- Returns:
- this
- Throws:
XMLDocumentException- If the current node has no child at all
-
gotoLastChild
Description copied from interface:XMLTagGo to the last child occurance found having given name- Specified by:
gotoLastChildin interfaceXMLTag- Parameters:
name- Name of the child to go at- Returns:
- this
- Throws:
XMLDocumentException- If the current node has no child at all
-
gotoTag
Description copied from interface:XMLTagGo to a specific node -
getCurrentTag
- Specified by:
getCurrentTagin interfaceXMLTag- Returns:
- the current tag
-
getChildCount
public int getChildCount()- Specified by:
getChildCountin interfaceXMLTag- Returns:
- The number of child for the current tag
-
getChilds
Description copied from interface:XMLTagXMLTag 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"); -
getChilds
Description copied from interface:XMLTagCreate an iterable object over selected elements. Act as the getChilds method: The current position of XMLTag is modified at each iteration. Thus, if you break in the iteration, the current position will not be the same as the position before. -
getChildElement
- Specified by:
getChildElementin interfaceXMLTag- Returns:
- The child element's list
-
getCurrentTagName
- Specified by:
getCurrentTagNamein interfaceXMLTag- Returns:
- the current tag name
-
getCurrentTagLocation
- Specified by:
getCurrentTagLocationin interfaceXMLTag- Returns:
- An XPath expression representing the current tag location in the document. If you are in the root node and run gotoTag() giving this XPath expression, you will return to the current node.
-
getAttribute
Description copied from interface:XMLTagreturns the attribute value of the current node- Specified by:
getAttributein interfaceXMLTag- Parameters:
name- attribute name- Returns:
- attribute value
-
findAttribute
Description copied from interface:XMLTagreturns the attribute value of the current node or null if the attribute does not exist- Specified by:
findAttributein interfaceXMLTag- Parameters:
name- attribute name- Returns:
- attribute value or null if no attribute
-
getAttribute
Description copied from interface:XMLTagReturns the attribute value of the node pointed by given XPath expression- Specified by:
getAttributein interfaceXMLTag- Parameters:
name- attribute namerelativeXpath- XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- attribute value
-
findAttribute
public String findAttribute(String name, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagReturns the attribute value of the node pointed by given XPath expression or null if the attribute does not exist- Specified by:
findAttributein interfaceXMLTag- Parameters:
name- attribute namerelativeXpath- XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- the attribute value or null if the attribute does not exist
- Throws:
XMLDocumentException- targetted node does not exist or XPath expression is invalid
-
getAttributeNames
- Specified by:
getAttributeNamesin interfaceXMLTag- Returns:
- all attribute names of current node
-
getText
Description copied from interface:XMLTagGet the text of a sepcific node -
getText
-
getTextOrCDATA
- Specified by:
getTextOrCDATAin interfaceXMLTag- Returns:
- The text content of the current node, if none tries to get the CDATA content, if none returns ""
-
getTextOrCDATA
Description copied from interface:XMLTagGet the text of a sepcific node- Specified by:
getTextOrCDATAin interfaceXMLTag- Parameters:
relativeXpath- XPath expression that select the nodearguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The text content of the current node, if none tries to get the CDATA content, if none returns ""
- Throws:
XMLDocumentException- If the XPath expression is not valid or if the node does not exist
-
getCDATAorText
- Specified by:
getCDATAorTextin interfaceXMLTag- Returns:
- The CDATA content of the current node, if none tries to get the text content, if none returns ""
-
getCDATAorText
Description copied from interface:XMLTagGet the text of a sepcific node- Specified by:
getCDATAorTextin interfaceXMLTag- Parameters:
relativeXpath- XPath expression that select the nodearguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- The CDATA content of the current node, if none tries to get the text content, if none returns ""
- Throws:
XMLDocumentException- If the XPath expression is not valid or if the node does not exist
-
getCDATA
Description copied from interface:XMLTagGet the CDATA of a selected node -
getCDATA
-
toDocument
- Specified by:
toDocumentin interfaceXMLTag- Returns:
- the Document
-
toSource
-
toString
-
toString
-
toBytes
-
toBytes
-
toStream
Description copied from interface:XMLTagWrite this document to a stream -
toStream
-
toStream
-
toStream
-
toResult
-
toResult
-
toOutputStream
- Specified by:
toOutputStreamin interfaceXMLTag- Returns:
- A stream where the document has already been written into
-
toOutputStream
- Specified by:
toOutputStreamin interfaceXMLTag- Parameters:
encoding- The new encoding- Returns:
- A stream where the document has already been written into
-
toWriter
-
toWriter
-
toResult
-
toResult
Description copied from interface:XMLTagConverts this document to the result provided, overriding default encoding of xml document -
validate
Description copied from interface:XMLTagValidate this document against specifief schemas -
validate
Description copied from interface:XMLTagValidate this document against specifief schemas -
isElement
-
childs
-
attr
-
childs
-
duplicate
-
setText
Description copied from interface:XMLTagSet the text in the current node. This method will replace all existing text and cdata by the given text. Also Jump after to the parent node. This enables the quicly replace test on several nodes like this:gotoChild("name").setText("Bob").gotoChild("sex").setText("M").gotoChild("age").setText("30")<name>Bob</name><sex>M</sex><age>30</age> -
setText
public XMLTag setText(String text, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagSet the text in the targetted node. This method will replace all existing text and cdata by the given text, but remains on the current tag.- Specified by:
setTextin interfaceXMLTag- Parameters:
text- text to put under this noderelativeXpath- XPath expression that select the nodearguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- this
- Throws:
XMLDocumentException- If the XPath expression is invalid or if the node does not exist
-
setTextIfExist
public XMLTag setTextIfExist(String text, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagSet the text in the targetted node. This method will replace all existing text and cdata by the given text, but remains on the current tag. If the targetted node does not exist, do nothing.- Specified by:
setTextIfExistin interfaceXMLTag- Parameters:
text- text to put under this noderelativeXpath- XPath expression that select the nodearguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- this
- Throws:
XMLDocumentException- If the XPath expression is invalid
-
setCDATA
Description copied from interface:XMLTagSet the cdata in the current node. This method will replace all existing text and cdata by the given cdata Also Jump after to the parent node. This enables the quicly replace test on several nodes like this:gotoChild("name").setText("Bob").gotoChild("sex").setText("M").gotoChild("age").setText("30")<name>Bob</name><sex>M</sex><age>30</age> -
setCDATA
public XMLTag setCDATA(String data, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagSet the cdata in the targetted node. This method will replace all existing text and cdata by the given cdata, but remains on the current tag.- Specified by:
setCDATAin interfaceXMLTag- Parameters:
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.- Returns:
- this
- Throws:
XMLDocumentException- If the XPath expression is invalid or if the node does not exist
-
setCDATAIfExist
public XMLTag setCDATAIfExist(String data, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagSet the cdata in the targetted node. This method will replace all existing text and cdata by the given cdata, but remains on the current tag. If the targetted node does not exist, do nothing.- Specified by:
setCDATAIfExistin interfaceXMLTag- Parameters:
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.- Returns:
- this
- Throws:
XMLDocumentException- If the XPath expression is invalid
-
setAttribute
Description copied from interface:XMLTagSets the new value on an existign attribute, and remains on the current tag.- Specified by:
setAttributein interfaceXMLTag- Parameters:
name- attribute namevalue- new attribute'svalue- Returns:
- attribute value
- Throws:
XMLDocumentException- If the attribute does not exist
-
setAttributeIfExist
Description copied from interface:XMLTagSets the new value on an attribute, and remains on the current tag. If it does not exist, do nothing.- Specified by:
setAttributeIfExistin interfaceXMLTag- Parameters:
name- attribute namevalue- new attribute value- Returns:
- attribute value
-
setAttribute
public XMLTag setAttribute(String name, String value, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagSets the new value on a targetted node's attribute, and remains on the current tag.- Specified by:
setAttributein interfaceXMLTag- Parameters:
name- attribute namevalue- new attribute's valuerelativeXpath- XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- attribute value
- Throws:
XMLDocumentException- Attribute does not exist, targetted node does not exit, or XPath expression is invalid
-
setAttributeIfExist
public XMLTag setAttributeIfExist(String name, String value, String relativeXpath, Object... arguments) throws XMLDocumentException Description copied from interface:XMLTagSets the new value on a targetted node's attribute, and remains on the current tag. If the attribute does not exist, do nothing.- Specified by:
setAttributeIfExistin interfaceXMLTag- Parameters:
name- attribute namevalue- new attribute's valuerelativeXpath- XPath expressionarguments- facultative Xpath expression arguments. Uses String.format() to build XPath expression.- Returns:
- attribute value
- Throws:
XMLDocumentException- XPath expression is invalid or targetted node does not exist
-
newDocument
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
newDocument
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
fromCurrentTag
Create anotherXMLTaginstance from the hierarchy under the current tag. The current tag becomes the root tag.- Parameters:
tag- The current XML Tag positionned to the new root tagignoreNamespaces- Wheter to build a namespace aware document- Returns:
- The inner XMLTag instance
-