Class XmlNode
java.lang.Object
com.aquima.interactions.foundation.xml.parsing.XmlNode
- All Implemented Interfaces:
IXmlNode
Simple XMLNode used to read elements from the DOM Parser. The XMLNode is a recursive (hierarchical) structure.
XMLNode is a wrapper around the standard org.w3c.dom.Node interface.
This cannot be used to create new nodes for use with the XMLDOMGenerator
, use
XMLNewElement
for that.
- Since:
- 5.0
- Author:
- IdJ
-
Constructor Summary
ConstructorsConstructorDescriptionCreate from a DOM Document, represents root element.Create from a DOM Document, represents root element.Create from a DOM Node, represents root element.Create from a DOM Node, represents root element. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Add a new child at the end of the existing child nodes of this node.protected void
Add a sibling child next to a given existing child (of this node).protected static void
collectChildren
(IXmlNode current, String path, Collection<IXmlNode> result) protected static IXmlNode
createElement
(Document xmldoc, String name, String stringValue) Create a new XMLNode (containing a new w3c DOM Node), optionally with a text value.protected IXmlNode
createXMLNode
(String name, String stringValue) Create a new XMLNode (containing a new w3c DOM Node), optionally with a text value.boolean
Two XMLNode objects are the same if their w3c node is the same.Finds a required XMLNode that occurs as a child (only one level deep) with the specified tagname.Finds an XMLNode that occurs as a child (only one level deep) with the specified tagname.findChildren
(String tagname) protected IXmlNode
findFirstChild
(String tagname) Get the first element with a certain XML tag name (starting at current element).Make a list of all XMLNodes with a certain XML tag name, recursively.getAttribute
(QualifiedName qualifiedName, boolean required) This method looksup an optional qualified attribute within an XML node.getAttribute
(String attr) Get the value of an attribute of this element.getAttribute
(String attribute, String fallback, boolean onEmpty) This method looksup an optional attribute within an XML node.IXmlNode[]
Returns the nodes of the attributes.Get all attributes of this element, as an ATTRNAME-to-VALUE mapping.getAttributeSafe
(String attr) Get the value of an attribute of this element.This method will return the child node indicated by the path.protected IXmlNode
IXmlNode[]
This method returns all the child element nodes of the current node.IXmlNode[]
getChildren
(String path) This method returns an array containing all the children that can be found matching the specified path.getChildSafe
(String path) This method will return the child node indicated by the path.Returns the first child of this element (only one level deep).Returns the first element child of this element (only one level deep).return the local node name.getName()
The name of the element (usually the XML tag).return the namespace (prefix) of the node.return the URI of the node's namespace.Get the next sibling node, that is, the next node on the same level as this node.Get the next sibling node, that is, the next node on the same level as this node.protected short
returns the parent node, raises XMLException if there is no parent.returns the parent node, or null if there is no parent.getTagValue
(String tagname) Get the value of the first element with a certain XML tag.getTagValues
(String tagname) Make a list of the values of all elements with a certain XML tag name, recursively.getValue()
The text 'value' of the element -- easy shortcut to get tag values.protected Document
protected Node
boolean
Shortcut method to quickly see if there are any attributes.int
hashCode()
hashCode needs to be overridden as well because of the equals() above.lookupNamespaceURI
(String prefix) Look up the namespace URI associated to the given prefix, starting from this node.protected void
remove all child nodes.protected void
removeChild
(XmlNode childNode) remove a child node.protected void
replaceChild
(XmlNode oldChild, XmlNode newChild) replace a child with a new child.void
Change the tag name.toString()
* Get some readable info on a DOM node - for debugging private String node2String(Node n) { if (n != null) { String type; switch (n.getNodeType()) { case Node.ELEMENT_NODE: type = "ELEMENT"; break; case Node.ATTRIBUTE_NODE: type = "ATTRIBUTE"; break; case Node.TEXT_NODE: type = "TEXT"; break; case Node.CDATA_SECTION_NODE: type = "CDATA_SECTION"; break; case Node.ENTITY_REFERENCE_NODE: type = "ENTITYREF"; break; case Node.ENTITY_NODE: type = "ENTITY"; break; case Node.PROCESSING_INSTRUCTION_NODE: type = "PROCESSING"; break; case Node.COMMENT_NODE: type = "COMMENT"; break; case Node.DOCUMENT_NODE: type = "DOCUMENT"; break; case Node.DOCUMENT_TYPE_NODE: type = "DOCTYPE"; break; case Node.DOCUMENT_FRAGMENT_NODE: type = "DOCFRAGMENT"; break; case Node.NOTATION_NODE: type = "NOTATION"; break; default: type = "??"; break; } return n.getNodeName() + " type=" + type + " #children=" + n.getChildNodes().getLength() + " value=" + n.getNodeValue(); } else return "null"; }protected void
toXmlfragment
(StringBuffer buffer, int i, Node node, boolean useIndentation) Easy way to create an XML string for this element and the children.
-
Constructor Details
-
XmlNode
Create from a DOM Document, represents root element.- Parameters:
doc
- the parsed XML document node
-
XmlNode
Create from a DOM Document, represents root element.- Parameters:
doc
- the parsed XML document nodetrimvalues
- true to trim all whitespace from element and attribute values, false to not trim it (default).- Throws:
XmlParseException
-
XmlNode
Create from a DOM Node, represents root element.- Parameters:
node
- The DOM node of the root element.
-
XmlNode
Create from a DOM Node, represents root element. With or without element and attribute value trimming.- Parameters:
node
- The DOM node of the root element.trimvalues
- Boolean indicating if white-spaces of values should be trimmed.
-
-
Method Details
-
createElement
Create a new XMLNode (containing a new w3c DOM Node), optionally with a text value.- Parameters:
xmldoc
- DOM Document where the node will be used in.name
- tagname of the new nodestringValue
- text value of the new node, use null for no value- Returns:
- new DOM Element
-
collectChildren
-
equals
Two XMLNode objects are the same if their w3c node is the same. -
hashCode
public int hashCode()hashCode needs to be overridden as well because of the equals() above. -
getName
Description copied from interface:IXmlNode
The name of the element (usually the XML tag). -
setName
Description copied from interface:IXmlNode
Change the tag name. WARNING: EXPENSIVE OPERATION ON W3C DOM TREE ELEMENTS!!!- Specified by:
setName
in interfaceIXmlNode
- Parameters:
name
- new name- Throws:
XmlParseException
- if name is null or invalid, or when trying to rename root element (sorry, not supported)
-
getNamespaceURI
Description copied from interface:IXmlNode
return the URI of the node's namespace. Requires a Namespace-aware DOM parser to work, otherwise returns null- Specified by:
getNamespaceURI
in interfaceIXmlNode
- Returns:
- the URI of the node's namespace.
-
getLocalName
Description copied from interface:IXmlNode
return the local node name. Requires a Namespace-aware DOM parser to work, otherwise returns null- Specified by:
getLocalName
in interfaceIXmlNode
- Returns:
- the local node name.
-
getNamespacePrefix
Description copied from interface:IXmlNode
return the namespace (prefix) of the node. Requires a Namespace-aware DOM parser to work, otherwise returns null- Specified by:
getNamespacePrefix
in interfaceIXmlNode
- Returns:
- the namespace (prefix) of the node.
-
lookupNamespaceURI
Description copied from interface:IXmlNode
Look up the namespace URI associated to the given prefix, starting from this node.- Specified by:
lookupNamespaceURI
in interfaceIXmlNode
- Returns:
- the namespace uri for the prefix
-
getValue
Description copied from interface:IXmlNode
The text 'value' of the element -- easy shortcut to get tag values. -
getAttribute
Description copied from interface:IXmlNode
Get the value of an attribute of this element.- Specified by:
getAttribute
in interfaceIXmlNode
- Parameters:
attr
- the attribute name to get the value of.- Returns:
- the value of the attribute
- Throws:
XmlParseException
- if attribute was not found- See Also:
-
getAttribute
Description copied from interface:IXmlNode
This method looksup an optional attribute within an XML node. If the attribute was not available the fallback value is taken. In addition the onEmpty can be used to count a zero-length (after trim) string as fallback as well.A node attribute specified as: attr="" will have the following results: node.getAttribute( "attr", "default", false ) => "" node.getAttribute( "attr", "default", true ) => "default"
- Specified by:
getAttribute
in interfaceIXmlNode
- Parameters:
attribute
- The attribute to look for in the xml node.fallback
- The fallback value to take when the attribute was not found or when 'onEmpty' the value was empty.onEmpty
- The onEmpty makes the getAttribute take the fallback value when the trimmed value string was empty as well, otherwise empty strings count as value.- Returns:
- The value according to the function, which can only be null when fallback is null.
-
getAttribute
Description copied from interface:IXmlNode
This method looksup an optional qualified attribute within an XML node.- Specified by:
getAttribute
in interfaceIXmlNode
- Parameters:
qualifiedName
- The qualified attribute to look for in the xml node.required
- Whether or not the attribute is required to be present- Returns:
- The attribute value, may be null when required=false
-
hasAttributes
public boolean hasAttributes()Description copied from interface:IXmlNode
Shortcut method to quickly see if there are any attributes.- Specified by:
hasAttributes
in interfaceIXmlNode
- Returns:
- Boolean indicating if the node has any attributes.
-
getAttributeSafe
Description copied from interface:IXmlNode
Get the value of an attribute of this element.- Specified by:
getAttributeSafe
in interfaceIXmlNode
- Parameters:
attr
- the attribute name to get the value of.- Returns:
- the value of the attribute (String), or null if the attribute doesn't exist.
- See Also:
-
getAttributeNodes
Description copied from interface:IXmlNode
Returns the nodes of the attributes.- Specified by:
getAttributeNodes
in interfaceIXmlNode
- Returns:
- an array containing the attribute nodes.
-
getAttributes
Description copied from interface:IXmlNode
Get all attributes of this element, as an ATTRNAME-to-VALUE mapping. If there are no attributes, returns an empty Map. This is faster than an ordered map!- Specified by:
getAttributes
in interfaceIXmlNode
- Returns:
- Map containing all the attributes names and values.
-
getTagValue
Description copied from interface:IXmlNode
Get the value of the first element with a certain XML tag.- Specified by:
getTagValue
in interfaceIXmlNode
- Parameters:
tagname
- the name of the tag- Returns:
- the value of the first tag with the given tagname
- Throws:
XmlParseException
- if no tag was found
-
findTag
Description copied from interface:IXmlNode
Get the first element with a certain XML tag name (starting at current element).- Specified by:
findTag
in interfaceIXmlNode
- Parameters:
tagname
- the name of the tag- Returns:
- the first XMLNode with the given tagname
- Throws:
XmlParseException
- if no tag was found
-
getTagValues
Description copied from interface:IXmlNode
Make a list of the values of all elements with a certain XML tag name, recursively. The list is constructed in preorder document traversal (depth-first) (this means: in the normal top-to-bottom 'reading' sequence that you expect)- Specified by:
getTagValues
in interfaceIXmlNode
- Parameters:
tagname
- the name of the tags you seek- Returns:
- a List containing all values of the tags you seek. It will be an empty list if no tags are found.
-
getFirstElement
Description copied from interface:IXmlNode
Returns the first element child of this element (only one level deep). This will skip comment nodes, entity nodes, #TEXT nodes etc.- Specified by:
getFirstElement
in interfaceIXmlNode
- Returns:
- the first element child of this element
- Throws:
XmlParseException
- if this element has no element children
-
getFirstChild
Description copied from interface:IXmlNode
Returns the first child of this element (only one level deep). This may be a comment node, entity node, #TEXT node etc. !!!!!- Specified by:
getFirstChild
in interfaceIXmlNode
- Returns:
- the first child node of this element
- Throws:
XmlParseException
- if this element has no child nodes
-
findChild
Description copied from interface:IXmlNode
Finds a required XMLNode that occurs as a child (only one level deep) with the specified tagname.- Specified by:
findChild
in interfaceIXmlNode
- Parameters:
tagname
- the name of the child- Returns:
- the element that is found
- Throws:
XmlParseException
- if no child was found
-
findChild
Description copied from interface:IXmlNode
Finds an XMLNode that occurs as a child (only one level deep) with the specified tagname.- Specified by:
findChild
in interfaceIXmlNode
- Parameters:
tagname
- the name of the childrequired
- true indicates that the child has to exist- Returns:
- the element that is found, or null if nothing is found and required==false
- Throws:
XmlParseException
- if no child was found and required == true
-
findFirstChild
- Throws:
XmlParseException
-
findChildren
-
findElements
-
getChildren
Description copied from interface:IXmlNode
This method returns all the child element nodes of the current node.- Specified by:
getChildren
in interfaceIXmlNode
- Returns:
- all the child element nodes of the current node.
-
getChildren
Description copied from interface:IXmlNode
This method returns an array containing all the children that can be found matching the specified path. When no children are available for the path, an empty array will be returned. The path may consist of multiple tags separated by a slash. For instance:<node> <foo> <bar>1</bar> </foo> <foo> <bar>2</bar> </foo> </node> node.getChildren( "foo/bar" );
The example above will return an array containing 2 elements (bar 1 & 2)- Specified by:
getChildren
in interfaceIXmlNode
- Parameters:
path
- The XML path for which the child nodes are requested.- Returns:
- An array containing all the children for the specified path, never null.
-
getParentSafe
Description copied from interface:IXmlNode
returns the parent node, or null if there is no parent.- Specified by:
getParentSafe
in interfaceIXmlNode
- Returns:
- The parent node of this element, or null if no parent is present.
-
getParent
Description copied from interface:IXmlNode
returns the parent node, raises XMLException if there is no parent.- Specified by:
getParent
in interfaceIXmlNode
- Returns:
- The parent node of this element (never null).
- Throws:
XmlParseException
-
getNextSibling
Description copied from interface:IXmlNode
Get the next sibling node, that is, the next node on the same level as this node.- Specified by:
getNextSibling
in interfaceIXmlNode
- Returns:
- the next sibling element
- Throws:
XmlParseException
- if no next sibling element was found.
-
getNextSiblingSafe
Description copied from interface:IXmlNode
Get the next sibling node, that is, the next node on the same level as this node.- Specified by:
getNextSiblingSafe
in interfaceIXmlNode
- Returns:
- the next sibling element, or null if no next sibling is found.
-
findTags
Make a list of all XMLNodes with a certain XML tag name, recursively. The list is constructed in preorder document traversal (depth-first). (this means: in the normal top-to-bottom 'reading' sequence that you expect)- Parameters:
tagname
- the name of the tags you seek- Returns:
- a List containing all XMLNodes with the tag name. It will be an empty list if no tags are found.
-
getNodeType
protected short getNodeType() -
getW3CRootNode
-
getW3CDocument
-
addChild
protected void addChild(XmlNode existingChild, XmlNode newChild, boolean before) throws XmlParseException Add a sibling child next to a given existing child (of this node).- Parameters:
existingChild
- existing child node to add a new node next tonewChild
- new node to addbefore
- set to true if you want to insert before existing child, otherwise the new sibling is inserted after it- Throws:
XmlParseException
- when the child node doesn't exist or when you try to create invalid XML
-
addChild
Add a new child at the end of the existing child nodes of this node.- Parameters:
child
- new child element- Throws:
XmlParseException
- when you try to create invalid XML
-
removeChild
remove a child node.- Parameters:
childNode
- node to remove- Throws:
XmlParseException
- if the child node didn't exist
-
removeAllChildren
protected void removeAllChildren()remove all child nodes. -
replaceChild
replace a child with a new child.- Parameters:
oldChild
- child node to replacenewChild
- child node to replace with- Throws:
XmlParseException
- if the child node did not exist, or if you try to create invalid XML
-
toString
* Get some readable info on a DOM node - for debugging private String node2String(Node n) { if (n != null) { String type; switch (n.getNodeType()) { case Node.ELEMENT_NODE: type = "ELEMENT"; break; case Node.ATTRIBUTE_NODE: type = "ATTRIBUTE"; break; case Node.TEXT_NODE: type = "TEXT"; break; case Node.CDATA_SECTION_NODE: type = "CDATA_SECTION"; break; case Node.ENTITY_REFERENCE_NODE: type = "ENTITYREF"; break; case Node.ENTITY_NODE: type = "ENTITY"; break; case Node.PROCESSING_INSTRUCTION_NODE: type = "PROCESSING"; break; case Node.COMMENT_NODE: type = "COMMENT"; break; case Node.DOCUMENT_NODE: type = "DOCUMENT"; break; case Node.DOCUMENT_TYPE_NODE: type = "DOCTYPE"; break; case Node.DOCUMENT_FRAGMENT_NODE: type = "DOCFRAGMENT"; break; case Node.NOTATION_NODE: type = "NOTATION"; break; default: type = "??"; break; } return n.getNodeName() + " type=" + type + " #children=" + n.getChildNodes().getLength() + " value=" + n.getNodeValue(); } else return "null"; } -
createXMLNode
Create a new XMLNode (containing a new w3c DOM Node), optionally with a text value.- Parameters:
name
- tagname of the new nodestringValue
- text value of the new node, use null for no value- Returns:
- new DOM Element
-
getChild
Description copied from interface:IXmlNode
This method will return the child node indicated by the path. When the path refers to multiple child elements, this method will raise an exception.- Specified by:
getChild
in interfaceIXmlNode
- Parameters:
path
- XML path identifying a single child node.- Returns:
- The child node for the specified path.
- Throws:
XmlException
- This exception is thrown when multiple or no node at all are/is found matching the path.
-
getChild
- Throws:
XmlException
-
getChildSafe
Description copied from interface:IXmlNode
This method will return the child node indicated by the path. When the path refers to multiple child elements, this method will raise an exception.- Specified by:
getChildSafe
in interfaceIXmlNode
- Parameters:
path
- XML path identifying a single child node.- Returns:
- The child node for the specified path.
- Throws:
XmlException
- This exception is thrown when multiple nodes are found matching the path.
-
toXmlFragment
Description copied from interface:IXmlNode
Easy way to create an XML string for this element and the children. Indent starts at 0. The toXMLfragment method operating on a StringBuffer is faster and better suited when creating only a part of a larger document. This method returns a string and was designed to get the "final" representation. It uses whitespace indentation for pretty printing.- Specified by:
toXmlFragment
in interfaceIXmlNode
- Returns:
- String containing the XML of this element and children.
-
toXmlfragment
-