toolbox.parsers
Class DOMParser

Package class diagram package DOMParser
java.lang.Object
  extended by toolbox.parsers.DOMParser

public final class DOMParser
extends java.lang.Object

Parser class to parse and access XML documents


Constructor Summary
DOMParser()
          Default constructor.
DOMParser(boolean nsAware)
          Create a namespace aware parser.
 
Method Summary
 java.lang.String concatTextNodes(org.w3c.dom.Node node)
          Concatenates the contents of all text() nodes in the given Node.
 java.util.Properties docToProps(org.w3c.dom.Document doc)
          Converts the contents of the given document into a java.util.Properties instance.
 java.util.Properties docToProps(org.w3c.dom.Document doc, java.lang.String delim)
          Converts the contents of the given document into a java.util.Properties instance using a customer delimiter.
 java.lang.String getAttrValue(org.w3c.dom.Node node, java.lang.String attrName)
          Returns the value of the named attribute in the given node.
 java.lang.String getAttrValue(java.lang.String elementName, java.lang.String attrName, org.w3c.dom.Document document)
          Returns the value of the named attribute in the named element of the given document.
 java.lang.String getAttrValue(java.lang.String elementName, java.lang.String attrName, org.w3c.dom.Document document, java.lang.String ns)
          Returns the value of the named attribute in the named element of the given document.
 org.w3c.dom.Element getElement(java.lang.String name, org.w3c.dom.Document doc)
          Returns the element with the given name or null if it does not exist.
 org.w3c.dom.Element getElement(java.lang.String name, org.w3c.dom.Document doc, java.lang.String ns)
          Returns the element with the given name or null if it does not exist.
 org.w3c.dom.Element getElement(java.lang.String elementName, org.w3c.dom.Element element)
          Returns the named element inside of the given element.
 org.w3c.dom.Element getElement(java.lang.String elementName, org.w3c.dom.Element element, java.lang.String ns)
          Returns the named element inside of the given element.
 org.w3c.dom.NodeList getElements(java.lang.String childName, org.w3c.dom.Element parent)
          Returns the children with the given name inside the given parent.
 org.w3c.dom.NodeList getElements(java.lang.String childName, org.w3c.dom.Element parent, java.lang.String ns)
          Returns the children with the given name inside the given parent.
 java.util.List getElementsByAttrValue(java.lang.String elementName, java.lang.String attrName, java.lang.String attrValue, org.w3c.dom.Element parent)
          Returns a List of nodes that have the same name and contains a named attribute with the same value.
 java.util.List getElementsByAttrValue(java.lang.String elementName, java.lang.String attrName, java.lang.String attrValue, org.w3c.dom.Element parent, java.lang.String ns)
          Returns a List of nodes that have the same name and contains a named attribute with the same value.
 java.lang.String getNodeName(org.w3c.dom.Node node)
          Returns the name of the given node.
 java.lang.String getTextNodeValue(org.w3c.dom.Node node)
          Returns the value of the given text node.
 java.lang.String getTextNodeValue(java.lang.String name, org.w3c.dom.Document doc)
          Returns the value of the named text node.
 java.lang.String getTextNodeValue(java.lang.String name, org.w3c.dom.Document doc, java.lang.String ns)
          Returns the value of the named text node.
 java.lang.String getTextNodeValue(java.lang.String name, org.w3c.dom.Node parent)
          Returns the value of the named text node within the given parent node.
 java.lang.String getTextNodeValue(java.lang.String name, org.w3c.dom.Node parent, java.lang.String ns)
          Returns the value of the named text node within the given parent node.
 java.lang.String getXMLStr(org.w3c.dom.Node node)
          Converts the given XML node to an XML string.
 java.util.Properties nodeToProps(org.w3c.dom.Node node)
          Converts the contents of the given node into a java.util.Properties instance.
 java.util.Properties nodeToProps(org.w3c.dom.Node node, java.lang.String delim)
          Converts the contents of the given node into a java.util.Properties instance using a customer delimiter.
 org.w3c.dom.Document parse(java.io.File file)
          Parses the file and returns a Document.
 org.w3c.dom.Document parse(java.io.InputStream is)
          Parse an InputStream and return a document.
 org.w3c.dom.Document parse(java.lang.String xmlStr)
          Parses an XML string and returns a Document.
 void setNodeValue(org.w3c.dom.Node node, java.lang.String attrName, java.lang.String attrValue)
          Sets the value of a specific attribute inside the given node.
 void setNodeValue(java.lang.String attrName, java.lang.String attrVal, org.w3c.dom.Node node)
          Sets the value of the named attribute within the given node.
 void setNodeValue(java.lang.String elementName, java.lang.String nodeName, java.lang.String nodeValue, org.w3c.dom.Document document)
          Sets the value of a specific attribute inside the named node of a specific DOM document.
 void setNodeValue(java.lang.String elementName, java.lang.String nodeName, java.lang.String nodeValue, org.w3c.dom.Document document, java.lang.String ns)
          Sets the value of a specific attribute inside the named node of a specific DOM document.
 void setTextNodeValue(org.w3c.dom.Node node, java.lang.String value)
          Sets the value of a text node.
 void setTextNodeValue(java.lang.String name, java.lang.String value, org.w3c.dom.Document doc)
          Sets the value of the named text node.
 void setTextNodeValue(java.lang.String name, java.lang.String value, org.w3c.dom.Document doc, java.lang.String ns)
          Sets the value of the named text node.
 void transform(java.io.InputStream xmlIn, java.io.InputStream xslIn, java.io.OutputStream out, java.util.Properties props)
          Simple method to perform an XSL transformation that writes the output to the given OutputStream.
 java.lang.String transform(java.lang.String xmlIn, java.lang.String xslIn, java.util.Properties props)
          Simple method to perform an XSL transformation that returns a String instance containing the results.
 void transform(java.lang.String xmlIn, java.lang.String xslIn, java.lang.String out, java.util.Properties props)
          Simple method to perform an XSL transformation that writes the output to a named file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOMParser

public DOMParser()
Default constructor.


DOMParser

public DOMParser(boolean nsAware)
Create a namespace aware parser.

Parameters:
nsAware - boolean indicating that the parse methods must be namespace aware.
Method Detail

parse

public org.w3c.dom.Document parse(java.io.InputStream is)
Parse an InputStream and return a document.

Parameters:
is - InputStream to parse.

parse

public org.w3c.dom.Document parse(java.lang.String xmlStr)
Parses an XML string and returns a Document.

Parameters:
xmlStr - XML string to parse.

parse

public org.w3c.dom.Document parse(java.io.File file)
Parses the file and returns a Document.

Parameters:
file - File to parse.

getElement

public org.w3c.dom.Element getElement(java.lang.String name,
                                      org.w3c.dom.Document doc)
Returns the element with the given name or null if it does not exist.

Parameters:
name - Element name.
doc - XML document.

getElement

public org.w3c.dom.Element getElement(java.lang.String name,
                                      org.w3c.dom.Document doc,
                                      java.lang.String ns)
Returns the element with the given name or null if it does not exist.

Parameters:
name - Element name.
doc - XML document.
ns - String containing the namespace uri.

getElement

public org.w3c.dom.Element getElement(java.lang.String elementName,
                                      org.w3c.dom.Element element)
Returns the named element inside of the given element.

Parameters:
elementName - The element name.
element - The given element in which to look for the named element.
Returns:
The specified element.

getElement

public org.w3c.dom.Element getElement(java.lang.String elementName,
                                      org.w3c.dom.Element element,
                                      java.lang.String ns)
Returns the named element inside of the given element.

Parameters:
elementName - The element name.
element - The given element in which to look for the named element.
ns - String containing the namespace uri.
Returns:
The specified element.

getElements

public org.w3c.dom.NodeList getElements(java.lang.String childName,
                                        org.w3c.dom.Element parent)
Returns the children with the given name inside the given parent.

Parameters:
childName - Name of child elements to return.
parent - Parent element.
Returns:
NodeList of child nodes with the given name.

getElements

public org.w3c.dom.NodeList getElements(java.lang.String childName,
                                        org.w3c.dom.Element parent,
                                        java.lang.String ns)
Returns the children with the given name inside the given parent.

Parameters:
childName - Name of child elements to return.
parent - Parent element.
ns - String containing the namespace uri.
Returns:
NodeList of child nodes with the given name.

setTextNodeValue

public void setTextNodeValue(java.lang.String name,
                             java.lang.String value,
                             org.w3c.dom.Document doc)
Sets the value of the named text node.

Parameters:
name - Text node name.
value - New text node value.
doc - XML document.

setTextNodeValue

public void setTextNodeValue(java.lang.String name,
                             java.lang.String value,
                             org.w3c.dom.Document doc,
                             java.lang.String ns)
Sets the value of the named text node.

Parameters:
name - Text node name.
value - New text node value.
doc - XML document.
ns - String containing the namespace uri.

getTextNodeValue

public java.lang.String getTextNodeValue(java.lang.String name,
                                         org.w3c.dom.Document doc)
Returns the value of the named text node.

Parameters:
name - Text node name.
doc - XML document.
Returns:
String containing the value of the named text node.

getTextNodeValue

public java.lang.String getTextNodeValue(java.lang.String name,
                                         org.w3c.dom.Document doc,
                                         java.lang.String ns)
Returns the value of the named text node.

Parameters:
name - Text node name.
doc - XML document.
ns - String containing the namespace uri.
Returns:
String containing the value of the named text node.

getTextNodeValue

public java.lang.String getTextNodeValue(org.w3c.dom.Node node)
Returns the value of the given text node.

Parameters:
node - Given text node.
Returns:
String containing the value of the given text node.

getTextNodeValue

public java.lang.String getTextNodeValue(java.lang.String name,
                                         org.w3c.dom.Node parent)
Returns the value of the named text node within the given parent node.

Parameters:
name - Name of the text node.
parent - Parent node.

getTextNodeValue

public java.lang.String getTextNodeValue(java.lang.String name,
                                         org.w3c.dom.Node parent,
                                         java.lang.String ns)
Returns the value of the named text node within the given parent node.

Parameters:
name - Name of the text node.
parent - Parent node.
ns - String containing the namespace uri.

setNodeValue

public void setNodeValue(java.lang.String attrName,
                         java.lang.String attrVal,
                         org.w3c.dom.Node node)
Sets the value of the named attribute within the given node.

Parameters:
attrName - Name of attribute.
attrVal - Attribute value.
node - Given node.

setNodeValue

public void setNodeValue(java.lang.String elementName,
                         java.lang.String nodeName,
                         java.lang.String nodeValue,
                         org.w3c.dom.Document document)
Sets the value of a specific attribute inside the named node of a specific DOM document.

Parameters:
elementName - The name of the element.
nodeName - The name of the node.
nodeValue - The new node value.
document - The DOM document.

setNodeValue

public void setNodeValue(java.lang.String elementName,
                         java.lang.String nodeName,
                         java.lang.String nodeValue,
                         org.w3c.dom.Document document,
                         java.lang.String ns)
Sets the value of a specific attribute inside the named node of a specific DOM document.

Parameters:
elementName - The name of the element.
nodeName - The name of the node.
nodeValue - The new node value.
document - The DOM document.
ns - String containing the namespace uri.

setNodeValue

public void setNodeValue(org.w3c.dom.Node node,
                         java.lang.String attrName,
                         java.lang.String attrValue)
Sets the value of a specific attribute inside the given node.

Parameters:
node - The given node.
attrName - The attribute name.
attrValue - The new attribute value.

getNodeName

public java.lang.String getNodeName(org.w3c.dom.Node node)
Returns the name of the given node.

Parameters:
node - The given node.
Returns:
String containing the name of node. The local name is returned if not null.

getAttrValue

public java.lang.String getAttrValue(java.lang.String elementName,
                                     java.lang.String attrName,
                                     org.w3c.dom.Document document)
Returns the value of the named attribute in the named element of the given document.

Parameters:
elementName - The element name.
attrName - The attribute name.
document - The given document.
Returns:
A String containing the attribute value.

getAttrValue

public java.lang.String getAttrValue(java.lang.String elementName,
                                     java.lang.String attrName,
                                     org.w3c.dom.Document document,
                                     java.lang.String ns)
Returns the value of the named attribute in the named element of the given document.

Parameters:
elementName - The element name.
attrName - The attribute name.
document - The given document.
ns - String containing the namespace uri.
Returns:
A String containing the attribute value.

getAttrValue

public java.lang.String getAttrValue(org.w3c.dom.Node node,
                                     java.lang.String attrName)
Returns the value of the named attribute in the given node.

Parameters:
node - The given node.
attrName - The attribute name.
Returns:
A String containing the attribute value.

setTextNodeValue

public void setTextNodeValue(org.w3c.dom.Node node,
                             java.lang.String value)
Sets the value of a text node.

Parameters:
node - The text node.
value - The new value of the text node.

getElementsByAttrValue

public java.util.List getElementsByAttrValue(java.lang.String elementName,
                                             java.lang.String attrName,
                                             java.lang.String attrValue,
                                             org.w3c.dom.Element parent)
Returns a List of nodes that have the same name and contains a named attribute with the same value.

Parameters:
elementName - Name of the element to look for.
attrName - Name of attribute.
attrValue - Value of attribute.
parent - Parent node of elementName.
Returns:
List containing result nodes.

getElementsByAttrValue

public java.util.List getElementsByAttrValue(java.lang.String elementName,
                                             java.lang.String attrName,
                                             java.lang.String attrValue,
                                             org.w3c.dom.Element parent,
                                             java.lang.String ns)
Returns a List of nodes that have the same name and contains a named attribute with the same value.

Parameters:
elementName - Name of the element to look for.
attrName - Name of attribute.
attrValue - Value of attribute.
parent - Parent node of elementName.
ns - String containing the namespace uri.
Returns:
List containing result nodes.

getXMLStr

public java.lang.String getXMLStr(org.w3c.dom.Node node)
Converts the given XML node to an XML string.

Parameters:
node - Node to convert.

concatTextNodes

public java.lang.String concatTextNodes(org.w3c.dom.Node node)
Concatenates the contents of all text() nodes in the given Node.

Parameters:
node - Root Node to start the concatenation from.

transform

public void transform(java.io.InputStream xmlIn,
                      java.io.InputStream xslIn,
                      java.io.OutputStream out,
                      java.util.Properties props)
               throws java.lang.Exception
Simple method to perform an XSL transformation that writes the output to the given OutputStream.

Parameters:
xmlIn - InputStream containing the XML to transform.
xslIn - InputStream containing the XSL transformation rules.
out - OutputStream where result must be written.
props - Properties instance containing stylesheet parameters.
Throws:
java.lang.Exception - if the transformation cannot be performed.

transform

public java.lang.String transform(java.lang.String xmlIn,
                                  java.lang.String xslIn,
                                  java.util.Properties props)
                           throws java.lang.Exception
Simple method to perform an XSL transformation that returns a String instance containing the results.

Parameters:
xmlIn - String containing the path to a file containing the XML to transform
xslIn - String containing the path to a file containing the XSL transformation rules.
props - Properties instance containing stylesheet parameters.
Returns:
String instance containing the results of the transformation.
Throws:
java.lang.Exception - if the transformation cannot be performed.

transform

public void transform(java.lang.String xmlIn,
                      java.lang.String xslIn,
                      java.lang.String out,
                      java.util.Properties props)
               throws java.lang.Exception
Simple method to perform an XSL transformation that writes the output to a named file.

Parameters:
xmlIn - String containing the path to a file containing the XML to transform
xslIn - String containing the path to a file containing the XSL transformation rules.
out - String containing the path to the output file.
props - Properties instance containing stylesheet parameters.
Throws:
java.lang.Exception - if the transformation cannot be performed.

docToProps

public java.util.Properties docToProps(org.w3c.dom.Document doc)
Converts the contents of the given document into a java.util.Properties instance.

This method does not use attribute names as property names, only element names are used.

Parameters:
doc - Document to convert.
Returns:
Properties instance.

docToProps

public java.util.Properties docToProps(org.w3c.dom.Document doc,
                                       java.lang.String delim)
Converts the contents of the given document into a java.util.Properties instance using a customer delimiter.

This method does not use attribute names as property names, only element names are used.

Parameters:
doc - Document to convert.
delim - String containing the property delimiter.
Returns:
Properties instance.

nodeToProps

public java.util.Properties nodeToProps(org.w3c.dom.Node node)
Converts the contents of the given node into a java.util.Properties instance.

This method does not use attribute names as property names, only element names are used.

Parameters:
node - Node to convert.
Returns:
Properties instance.

nodeToProps

public java.util.Properties nodeToProps(org.w3c.dom.Node node,
                                        java.lang.String delim)
Converts the contents of the given node into a java.util.Properties instance using a customer delimiter.

This method does not use attribute names as property names, only element names are used.

Parameters:
node - Node to convert.
delim - String containing the property delimiter.
Returns:
Properties instance.