com.mycila.xmltool
Class XMLDoc

java.lang.Object
  extended by com.mycila.xmltool.XMLDoc
All Implemented Interfaces:
XMLTag

public final class XMLDoc
extends Object
implements XMLTag

Author:
Mathieu Carbou (mathieu.carbou@gmail.com)

Method Summary
 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.
static XMLTag from(File file, boolean ignoreNamespaces)
           
static XMLTag from(InputSource source, boolean ignoreNamespaces)
           
static XMLTag from(InputStream is, boolean ignoreNamespaces)
           
static XMLTag from(Node node, boolean ignoreNamespaces)
           
static XMLTag from(Reader reader, boolean ignoreNamespaces)
           
static XMLTag from(Source source, boolean ignoreNamespaces)
           
static XMLTag from(String xmlData, boolean ignoreNamespaces)
           
static XMLTag from(URL xmlLocation, boolean ignoreNamespaces)
           
static XMLTag from(XMLTag tag, boolean ignoreNamespaces)
           
static XMLTag fromCurrentTag(XMLTag tag, boolean ignoreNamespaces)
          Create another XMLTag instance from the hierarchy under the current tag.
 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
static XMLDocBuilder newDocument(boolean ignoreNamespaces)
           
 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 newNodeName)
          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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getContext

public NamespaceContext getContext()
Specified by:
getContext in interface XMLTag
Returns:
the namespace context

hasAttribute

public boolean hasAttribute(String name)
Description copied from interface: XMLTag
Check wheter current tag contains an atribute

Specified by:
hasAttribute in interface XMLTag
Parameters:
name - Attribute name
Returns:
true if the attribute is in current tag

hasAttribute

public boolean hasAttribute(String name,
                            String relativeXpath,
                            Object... arguments)
Description copied from interface: XMLTag
Check if targeted tag has an attribute of given name

Specified by:
hasAttribute in interface XMLTag
Parameters:
name - the name of the attribute
relativeXpath - XPath that target the tag
arguments - optional arguments of xpath expression. Uses String.format() to build XPath expression.
Returns:
true if the tag exist with this attribute name

hasTag

public boolean hasTag(String relativeXpath,
                      Object... arguments)
Description copied from interface: XMLTag
Check if a tag exist in the document

Specified by:
hasTag in interface XMLTag
Parameters:
relativeXpath - XPath expression where the tag should be located
arguments - XPath arguments. Uses String.format() to build XPath expression.
Returns:
true if the tag is exist

forEachChild

public XMLTag forEachChild(CallBack callBack)
Description copied from interface: XMLTag
Execute an action for each child in the current node.

Specified by:
forEachChild in interface XMLTag
Parameters:
callBack - Callback method to run after the current tag of the document has changed to a child
Returns:
this

forEach

public XMLTag forEach(CallBack callBack,
                      String relativeXpath,
                      Object... arguments)
Description copied from interface: XMLTag
Execute an action for each selected tags from the current node.

Specified by:
forEach in interface XMLTag
Parameters:
callBack - Callback method to run after the current tag of the document has changed to a child
relativeXpath - XXath expression that select the node
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
this

forEach

public XMLTag forEach(String xpath,
                      CallBack callBack)
Specified by:
forEach in interface XMLTag

rawXpathString

public String rawXpathString(String relativeXpath,
                             Object... arguments)
Description copied from interface: XMLTag
Execute an XPath expression directly using the Java XPath API, from the current node.

Specified by:
rawXpathString in interface XMLTag
Parameters:
relativeXpath - The XPath expression
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
The XPathConstants.STRING return type

rawXpathNumber

public Number rawXpathNumber(String relativeXpath,
                             Object... arguments)
Description copied from interface: XMLTag
Execute an XPath expression directly using the Java XPath API, from the current node.

Specified by:
rawXpathNumber in interface XMLTag
Parameters:
relativeXpath - The XPath expression
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
The XPathConstants.NUMBER return type

rawXpathBoolean

public Boolean rawXpathBoolean(String relativeXpath,
                               Object... arguments)
Description copied from interface: XMLTag
Execute an XPath expression directly using the Java XPath API, from the current node.

Specified by:
rawXpathBoolean in interface XMLTag
Parameters:
relativeXpath - The XPath expression
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
The XPathConstants.BOOLEAN return type

rawXpathNode

public Node rawXpathNode(String relativeXpath,
                         Object... arguments)
Description copied from interface: XMLTag
Execute an XPath expression directly using the Java XPath API, from the current node.

Specified by:
rawXpathNode in interface XMLTag
Parameters:
relativeXpath - The XPath expression
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
The XPathConstants.NODE return type

rawXpathNodeSet

public NodeList rawXpathNodeSet(String relativeXpath,
                                Object... arguments)
Description copied from interface: XMLTag
Execute an XPath expression directly using the Java XPath API, from the current node.

Specified by:
rawXpathNodeSet in interface XMLTag
Parameters:
relativeXpath - The XPath expression
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
The XPathConstants.NODESET return type

getPefix

public String getPefix(String namespaceURI)
Description copied from interface: XMLTag
Get the prefix of a namespace

Specified by:
getPefix in interface XMLTag
Parameters:
namespaceURI - The URI of the namespace
Returns:
the prefix or "" if not found ("" is the default prefix - see javadoc)

getPefixes

public String[] getPefixes(String namespaceURI)
Description copied from interface: XMLTag
Get all bound prefixes of a namespace

Specified by:
getPefixes in interface XMLTag
Parameters:
namespaceURI - The URI of the namespace
Returns:
a list of prefixes or an empty array.

addNamespace

public XMLTag addNamespace(String prefix,
                           String namespaceURI)
Description copied from interface: XMLTag
Add a namespace to the document

Specified by:
addNamespace in interface XMLTag
Parameters:
prefix - The prefix of the namespace
namespaceURI - The URI of the namespace
Returns:
this

addDocument

public XMLTag addDocument(XMLTag tag)
Description copied from interface: XMLTag
Inserts another XMLTag instance under the current tag. The whole document will be inserted.

Specified by:
addDocument in interface XMLTag
Parameters:
tag - The XMLTag instance to insert
Returns:
this

addDocument

public XMLTag addDocument(Document doc)
Description copied from interface: XMLTag
Inserts another Document instance under the current tag

Specified by:
addDocument in interface XMLTag
Parameters:
doc - The Document instance to insert
Returns:
this

addTag

public XMLTag addTag(XMLTag tag)
Description copied from interface: XMLTag
Inserts another 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.

Specified by:
addTag in interface XMLTag
Parameters:
tag - The XMLTag current tag hierarchy to insert
Returns:
this

addTag

public XMLTag addTag(Element tag)
Description copied from interface: XMLTag
Inserts a Element instance and its hierarchy under the current tag

Specified by:
addTag in interface XMLTag
Parameters:
tag - The Element instance to insert
Returns:
this

addTag

public XMLTag addTag(String name)
Description copied from interface: XMLTag
Create a tag under the current location and use it as the current node

Specified by:
addTag in interface XMLTag
Parameters:
name - Name of the element to add
Returns:
this

addAttribute

public XMLTag addAttribute(String name,
                           String value)
Description copied from interface: XMLTag
Create a new attribute for the current node

Specified by:
addAttribute in interface XMLTag
Parameters:
name - Name of the attribute to add
value - value of the attribute to add
Returns:
this

addAttribute

public XMLTag addAttribute(Attr attr)
Description copied from interface: XMLTag
Add given attribute to current element

Specified by:
addAttribute in interface XMLTag
Parameters:
attr - The attribute to insert
Returns:
this

addText

public XMLTag addText(String text)
Description copied from interface: XMLTag
Add 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>

Specified by:
addText in interface XMLTag
Parameters:
text - the text to add
Returns:
this

addText

public XMLTag addText(Text text)
Description copied from interface: XMLTag
Add a text note to the current tag

Specified by:
addText in interface XMLTag
Parameters:
text - The node to insert
Returns:
this

addCDATA

public XMLTag addCDATA(String data)
Description copied from interface: XMLTag
Add 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>

Specified by:
addCDATA in interface XMLTag
Parameters:
data - the data to add
Returns:
this

addCDATA

public XMLTag addCDATA(CDATASection data)
Description copied from interface: XMLTag
Add a CDATA note to the current tag

Specified by:
addCDATA in interface XMLTag
Parameters:
data - The node to insert
Returns:
this

delete

public XMLTag delete()
Description copied from interface: XMLTag
Delete current tag and its childs. If the current tag is the root tag of xml document, it cannot be delete. It can just be replaced. In this case, an exception is thrown

Specified by:
delete in interface XMLTag
Returns:
this

deleteChilds

public XMLTag deleteChilds()
Description copied from interface: XMLTag
Delete all existing elements of this node

Specified by:
deleteChilds in interface XMLTag
Returns:
this

deleteAttributes

public XMLTag deleteAttributes()
Description copied from interface: XMLTag
Delete all existing attributes of current node

Specified by:
deleteAttributes in interface XMLTag
Returns:
this

deleteAttribute

public XMLTag deleteAttribute(String name)
Description copied from interface: XMLTag
Delete an attribute of the current node.

Specified by:
deleteAttribute in interface XMLTag
Parameters:
name - attribute name
Returns:
this

deleteAttributeIfExists

public XMLTag deleteAttributeIfExists(String name)
Description copied from interface: XMLTag
Delete an attribute of the current node, if it exists

Specified by:
deleteAttributeIfExists in interface XMLTag
Parameters:
name - attribute name
Returns:
this

renameTo

public XMLTag renameTo(String newNodeName)
Description copied from interface: XMLTag
Replace current element name by another name

Specified by:
renameTo in interface XMLTag
Parameters:
newNodeName - New name of the tag
Returns:
this

deletePrefixes

public XMLTag deletePrefixes()
Description copied from interface: XMLTag
Remove 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:
deletePrefixes in interface XMLTag
Returns:
this

getInnerDocument

public XMLTag getInnerDocument()
Specified by:
getInnerDocument in interface XMLTag
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

public String getInnerText()
Specified by:
getInnerText in interface XMLTag
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

public XMLTag gotoParent()
Description copied from interface: XMLTag
Go to parent tag. Do nothing if we are already at root

Specified by:
gotoParent in interface XMLTag
Returns:
this

gotoRoot

public XMLTag gotoRoot()
Description copied from interface: XMLTag
Go to document root tag

Specified by:
gotoRoot in interface XMLTag
Returns:
this

gotoChild

public XMLTag gotoChild()
Description copied from interface: XMLTag
Go to the only child element of the curent node.

Specified by:
gotoChild in interface XMLTag
Returns:
this

gotoChild

public XMLTag gotoChild(int i)
Description copied from interface: XMLTag
Go to the Nth child of the curent node.

Specified by:
gotoChild in interface XMLTag
Parameters:
i - index of the child, from 1 to child element number
Returns:
this

gotoChild

public XMLTag gotoChild(String nodeName)
Description copied from interface: XMLTag
Go to the child found with given node name

Specified by:
gotoChild in interface XMLTag
Parameters:
nodeName - name of the child to find.
Returns:
this

gotoFirstChild

public XMLTag gotoFirstChild()
                      throws XMLDocumentException
Description copied from interface: XMLTag
Go to the first child element of the curent node.

Specified by:
gotoFirstChild in interface XMLTag
Returns:
this
Throws:
XMLDocumentException - If the current node has no child at all

gotoFirstChild

public XMLTag gotoFirstChild(String name)
                      throws XMLDocumentException
Description copied from interface: XMLTag
Go to the first child occurance found having given name

Specified by:
gotoFirstChild in interface XMLTag
Parameters:
name - Name of the child to go at
Returns:
this
Throws:
XMLDocumentException - If the current node has no child at all

gotoLastChild

public XMLTag gotoLastChild()
                     throws XMLDocumentException
Description copied from interface: XMLTag
Go to the lastest child element of the curent node.

Specified by:
gotoLastChild in interface XMLTag
Returns:
this
Throws:
XMLDocumentException - If the current node has no child at all

gotoLastChild

public XMLTag gotoLastChild(String name)
                     throws XMLDocumentException
Description copied from interface: XMLTag
Go to the last child occurance found having given name

Specified by:
gotoLastChild in interface XMLTag
Parameters:
name - Name of the child to go at
Returns:
this
Throws:
XMLDocumentException - If the current node has no child at all

gotoTag

public XMLTag gotoTag(String relativeXpath,
                      Object... arguments)
Description copied from interface: XMLTag
Go to a specific node

Specified by:
gotoTag in interface XMLTag
Parameters:
relativeXpath - XPath expresion
arguments - to be replaced in xpath expression before compiling. Uses String.format() to build XPath expression.
Returns:
this

getCurrentTag

public Element getCurrentTag()
Specified by:
getCurrentTag in interface XMLTag
Returns:
the current tag

getChildCount

public int getChildCount()
Specified by:
getChildCount in interface XMLTag
Returns:
The number of child for the current tag

getChilds

public Iterable<XMLTag> getChilds()
Description copied from interface: XMLTag
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");

Specified by:
getChilds in interface XMLTag
Returns:
An iterable object over childs

getChilds

public Iterable<XMLTag> getChilds(String relativeXpath,
                                  Object... arguments)
Description copied from interface: XMLTag
Create 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.

Specified by:
getChilds in interface XMLTag
Parameters:
relativeXpath - XPath to select tags
arguments - XPath arguments
Returns:
The iterable object

getChildElement

public List<Element> getChildElement()
Specified by:
getChildElement in interface XMLTag
Returns:
The child element's list

getCurrentTagName

public String getCurrentTagName()
Specified by:
getCurrentTagName in interface XMLTag
Returns:
the current tag name

getCurrentTagLocation

public String getCurrentTagLocation()
Specified by:
getCurrentTagLocation in interface XMLTag
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

public String getAttribute(String name)
Description copied from interface: XMLTag
returns the attribute value of the current node

Specified by:
getAttribute in interface XMLTag
Parameters:
name - attribute name
Returns:
attribute value

findAttribute

public String findAttribute(String name)
Description copied from interface: XMLTag
returns the attribute value of the current node or null if the attribute does not exist

Specified by:
findAttribute in interface XMLTag
Parameters:
name - attribute name
Returns:
attribute value or null if no attribute

getAttribute

public String getAttribute(String name,
                           String relativeXpath,
                           Object... arguments)
Description copied from interface: XMLTag
Returns the attribute value of the node pointed by given XPath expression

Specified by:
getAttribute in interface XMLTag
Parameters:
name - attribute name
relativeXpath - XPath expression
arguments - 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: XMLTag
Returns the attribute value of the node pointed by given XPath expression or null if the attribute does not exist

Specified by:
findAttribute in interface XMLTag
Parameters:
name - attribute name
relativeXpath - XPath expression
arguments - 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

public String[] getAttributeNames()
Specified by:
getAttributeNames in interface XMLTag
Returns:
all attribute names of current node

getText

public String getText(String relativeXpath,
                      Object... arguments)
Description copied from interface: XMLTag
Get the text of a sepcific node

Specified by:
getText in interface XMLTag
Parameters:
relativeXpath - XPath expression that select the node
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
the text of "" if none

getText

public String getText()
Specified by:
getText in interface XMLTag
Returns:
The text content of the current node, "" if none

getTextOrCDATA

public String getTextOrCDATA()
Specified by:
getTextOrCDATA in interface XMLTag
Returns:
The text content of the current node, if none tries to get the CDATA content, if none returns ""

getTextOrCDATA

public String getTextOrCDATA(String relativeXpath,
                             Object... arguments)
                      throws XMLDocumentException
Description copied from interface: XMLTag
Get the text of a sepcific node

Specified by:
getTextOrCDATA in interface XMLTag
Parameters:
relativeXpath - XPath expression that select the node
arguments - 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

public String getCDATAorText()
Specified by:
getCDATAorText in interface XMLTag
Returns:
The CDATA content of the current node, if none tries to get the text content, if none returns ""

getCDATAorText

public String getCDATAorText(String relativeXpath,
                             Object... arguments)
                      throws XMLDocumentException
Description copied from interface: XMLTag
Get the text of a sepcific node

Specified by:
getCDATAorText in interface XMLTag
Parameters:
relativeXpath - XPath expression that select the node
arguments - 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

public String getCDATA(String relativeXpath,
                       Object... arguments)
Description copied from interface: XMLTag
Get the CDATA of a selected node

Specified by:
getCDATA in interface XMLTag
Parameters:
relativeXpath - XPath expression that select the node
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
the text of "" if none

getCDATA

public String getCDATA()
Specified by:
getCDATA in interface XMLTag
Returns:
The CDATA content of the current node, "" if none

toDocument

public Document toDocument()
Specified by:
toDocument in interface XMLTag
Returns:
the Document

toSource

public Source toSource()
Specified by:
toSource in interface XMLTag
Returns:
This document as Source

toString

public String toString()
Specified by:
toString in interface XMLTag
Overrides:
toString in class Object
Returns:
a string representation of the document in UTF-8 (Unicode)

toString

public String toString(String encoding)
Specified by:
toString in interface XMLTag
Parameters:
encoding - destination encoding of XML document
Returns:
a string representation of the document

toBytes

public byte[] toBytes()
Specified by:
toBytes in interface XMLTag
Returns:
This document representation as String bytes, using default encoding of the document

toBytes

public byte[] toBytes(String encoding)
Specified by:
toBytes in interface XMLTag
Parameters:
encoding - The encoding to use
Returns:
This document representation as String bytes using sepcified ancoding

toStream

public XMLTag toStream(OutputStream out)
Description copied from interface: XMLTag
Write this document to a stream

Specified by:
toStream in interface XMLTag
Parameters:
out - The output result
Returns:
this

toStream

public XMLTag toStream(OutputStream out,
                       String encoding)
Description copied from interface: XMLTag
Write this document to a stream

Specified by:
toStream in interface XMLTag
Parameters:
out - The output result
encoding - The new encoding
Returns:
this

toStream

public XMLTag toStream(Writer out)
Description copied from interface: XMLTag
Write this document to a stream

Specified by:
toStream in interface XMLTag
Parameters:
out - The output result
Returns:
this

toStream

public XMLTag toStream(Writer out,
                       String encoding)
Description copied from interface: XMLTag
Write this document to a stream

Specified by:
toStream in interface XMLTag
Parameters:
out - The output result
encoding - The new encoding
Returns:
this

toResult

public Result toResult()
Specified by:
toResult in interface XMLTag
Returns:
The Result representation of this document. Useful when using web services for example.

toResult

public Result toResult(String encoding)
Specified by:
toResult in interface XMLTag
Parameters:
encoding - The new encoding
Returns:
The Result representation of this document. Useful when using web services for example.

toOutputStream

public OutputStream toOutputStream()
Specified by:
toOutputStream in interface XMLTag
Returns:
A stream where the document has already been written into

toOutputStream

public OutputStream toOutputStream(String encoding)
Specified by:
toOutputStream in interface XMLTag
Parameters:
encoding - The new encoding
Returns:
A stream where the document has already been written into

toWriter

public Writer toWriter()
Specified by:
toWriter in interface XMLTag
Returns:
A stream where the document has already been written into

toWriter

public Writer toWriter(String encoding)
Specified by:
toWriter in interface XMLTag
Parameters:
encoding - The new encoding
Returns:
A stream where the document has already been written into

toResult

public XMLTag toResult(Result out)
Description copied from interface: XMLTag
Converts this document to the result provided

Specified by:
toResult in interface XMLTag
Parameters:
out - The output result
Returns:
this

toResult

public XMLTag toResult(Result out,
                       String encoding)
Description copied from interface: XMLTag
Converts this document to the result provided, overriding default encoding of xml document

Specified by:
toResult in interface XMLTag
Parameters:
out - The output result
encoding - The new encoding
Returns:
this

validate

public ValidationResult validate(Source... schemas)
Description copied from interface: XMLTag
Validate this document against specifief schemas

Specified by:
validate in interface XMLTag
Parameters:
schemas - A list of schemas
Returns:
A validation result object containing exception list occured if any

validate

public ValidationResult validate(URL... schemaLocations)
Description copied from interface: XMLTag
Validate this document against specifief schemas

Specified by:
validate in interface XMLTag
Parameters:
schemaLocations - A list of schemas
Returns:
A validation result object containing exception list occured if any

duplicate

public XMLTag duplicate()
Specified by:
duplicate in interface XMLTag
Returns:
A new com.mycila.xmltool.XMLTag instance having the same properties and documents of the current instance. The current tag will also remain the same (this.getCurrentTagName().equals(this.duplicate().getCurrentTagName()))

setText

public XMLTag setText(String text)
Description copied from interface: XMLTag
Set 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>

Specified by:
setText in interface XMLTag
Parameters:
text - text to put under this node
Returns:
this

setText

public XMLTag setText(String text,
                      String relativeXpath,
                      Object... arguments)
               throws XMLDocumentException
Description copied from interface: XMLTag
Set 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:
setText in interface XMLTag
Parameters:
text - text to put under this node
relativeXpath - XPath expression that select the node
arguments - 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: XMLTag
Set 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:
setTextIfExist in interface XMLTag
Parameters:
text - text to put under this node
relativeXpath - XPath expression that select the node
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
this
Throws:
XMLDocumentException - If the XPath expression is invalid

setCDATA

public XMLTag setCDATA(String data)
Description copied from interface: XMLTag
Set 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>

Specified by:
setCDATA in interface XMLTag
Parameters:
data - text to put under this node in a cdata section
Returns:
this

setCDATA

public XMLTag setCDATA(String data,
                       String relativeXpath,
                       Object... arguments)
                throws XMLDocumentException
Description copied from interface: XMLTag
Set 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:
setCDATA in interface XMLTag
Parameters:
data - text to put under this node in a cdata section
relativeXpath - XPath expression that select the node
arguments - 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: XMLTag
Set 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:
setCDATAIfExist in interface XMLTag
Parameters:
data - text to put under this node in a cdata section
relativeXpath - XPath expression that select the node
arguments - facultative Xpath expression arguments. Uses String.format() to build XPath expression.
Returns:
this
Throws:
XMLDocumentException - If the XPath expression is invalid

setAttribute

public XMLTag setAttribute(String name,
                           String value)
                    throws XMLDocumentException
Description copied from interface: XMLTag
Sets the new value on an existign attribute, and remains on the current tag.

Specified by:
setAttribute in interface XMLTag
Parameters:
name - attribute name
value - new attribute'svalue
Returns:
attribute value
Throws:
XMLDocumentException - If the attribute does not exist

setAttributeIfExist

public XMLTag setAttributeIfExist(String name,
                                  String value)
Description copied from interface: XMLTag
Sets the new value on an attribute, and remains on the current tag. If it does not exist, do nothing.

Specified by:
setAttributeIfExist in interface XMLTag
Parameters:
name - attribute name
value - new attribute value
Returns:
attribute value

setAttribute

public XMLTag setAttribute(String name,
                           String value,
                           String relativeXpath,
                           Object... arguments)
                    throws XMLDocumentException
Description copied from interface: XMLTag
Sets the new value on a targetted node's attribute, and remains on the current tag.

Specified by:
setAttribute in interface XMLTag
Parameters:
name - attribute name
value - new attribute's value
relativeXpath - XPath expression
arguments - 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: XMLTag
Sets 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:
setAttributeIfExist in interface XMLTag
Parameters:
name - attribute name
value - new attribute's value
relativeXpath - XPath expression
arguments - 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

public static XMLDocBuilder newDocument(boolean ignoreNamespaces)

from

public static XMLTag from(Node node,
                          boolean ignoreNamespaces)

from

public static XMLTag from(InputSource source,
                          boolean ignoreNamespaces)

from

public static XMLTag from(Reader reader,
                          boolean ignoreNamespaces)

from

public static XMLTag from(InputStream is,
                          boolean ignoreNamespaces)

from

public static XMLTag from(File file,
                          boolean ignoreNamespaces)

from

public static XMLTag from(URL xmlLocation,
                          boolean ignoreNamespaces)

from

public static XMLTag from(String xmlData,
                          boolean ignoreNamespaces)

from

public static XMLTag from(Source source,
                          boolean ignoreNamespaces)

from

public static XMLTag from(XMLTag tag,
                          boolean ignoreNamespaces)

fromCurrentTag

public static XMLTag fromCurrentTag(XMLTag tag,
                                    boolean ignoreNamespaces)
Create another XMLTag instance 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 tag
ignoreNamespaces - Wheter to build a namespace aware document
Returns:
The inner XMLTag instance


Copyright © 2008-2011 Mathieu Carbou. All Rights Reserved.