edu.northwestern.at.utils.xml
Class DOMUtils

java.lang.Object
  extended by edu.northwestern.at.utils.xml.DOMUtils

public class DOMUtils
extends java.lang.Object

XML DOM utilities.


Method Summary
static java.lang.String getAllText(org.w3c.dom.Node node)
          Gets all the text for a node.
static org.w3c.dom.Element getChild(org.w3c.dom.Node node, java.lang.String name)
          Gets a child element of a node by name.
static org.w3c.dom.Element getChild(org.w3c.dom.Node node, java.lang.String name, java.lang.String attrName, java.lang.String attrValue)
          Gets a child element of a node by name and attribute value.
static java.util.ArrayList getChildren(org.w3c.dom.Node node, java.lang.String name)
          Gets the child elements of a node by name.
static java.util.ArrayList getChildren(org.w3c.dom.Node node, java.lang.String name, java.lang.String attrName, java.lang.String attrValue)
          Gets the child elements of a node by name and attribute value.
static org.w3c.dom.Element getDescendant(org.w3c.dom.Node node, java.lang.String path)
          Gets a descendant element of a node.
static java.util.ArrayList getDescendants(org.w3c.dom.Node node, java.lang.String path)
          Gets descendant elements of a node.
static org.w3c.dom.Element getLastChild(org.w3c.dom.Node node, java.lang.String name)
          Gets the last child element of a node by name.
static java.lang.String getText(org.w3c.dom.Node node)
          Gets text for a node.
static org.w3c.dom.Document newDocument()
          Creates a new empty DOM document.
static boolean nodeHasChild(org.w3c.dom.Node node, java.lang.String name)
          Checks to see if a node has a child of some name.
static boolean nodeHasDescendant(org.w3c.dom.Node node, java.lang.String name)
          Checks to see if a node has a descendant of some name.
static boolean nodeHasDescendant(org.w3c.dom.Node node, java.lang.String[] names1, java.lang.String[] names2)
          Checks to see if a node has a descendant node of some name, other than those in children of some other name.
static boolean nodeIsOrHasChild(org.w3c.dom.Node node, java.lang.String name)
          Checks to see if a node has a given name or has a child of the given name.
static boolean nodeIsOrHasDescendant(org.w3c.dom.Node node, java.lang.String name)
          Checks to see if a node has a given name or has a descendant of the given name.
static org.w3c.dom.Document parse(java.io.File file)
          Parses an XML file.
static org.w3c.dom.Document parse(java.lang.String path)
          Parses an XML file.
static org.w3c.dom.Document parse(java.net.URL url)
          Parses XML document from URL.
static void save(org.w3c.dom.Document document, java.lang.String path)
          Saves a DOM document to an XML file in utf-8.
static void setText(org.w3c.dom.Node node, java.lang.String text)
          Sets text for a node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parse

public static org.w3c.dom.Document parse(java.io.File file)
                                  throws java.io.IOException,
                                         javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException
Parses an XML file.

Parameters:
file - File.
Returns:
DOM document.
Throws:
java.lang.Exception
java.io.IOException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

parse

public static org.w3c.dom.Document parse(java.lang.String path)
                                  throws java.io.IOException,
                                         javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException
Parses an XML file.

Parameters:
path - File path.
Returns:
DOM document.
Throws:
java.lang.Exception
java.io.IOException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

parse

public static org.w3c.dom.Document parse(java.net.URL url)
                                  throws java.io.IOException,
                                         javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException
Parses XML document from URL.

Parameters:
url - URL.
Returns:
DOM document.
Throws:
java.lang.Exception
java.io.IOException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

getChild

public static org.w3c.dom.Element getChild(org.w3c.dom.Node node,
                                           java.lang.String name)
Gets a child element of a node by name.

Parameters:
node - Node.
name - Name.
Returns:
First child element with given tag name, or null if none found.

getLastChild

public static org.w3c.dom.Element getLastChild(org.w3c.dom.Node node,
                                               java.lang.String name)
Gets the last child element of a node by name.

Parameters:
node - Node.
name - Name.
Returns:
Last child element with given tag name, or null if none found.

getChild

public static org.w3c.dom.Element getChild(org.w3c.dom.Node node,
                                           java.lang.String name,
                                           java.lang.String attrName,
                                           java.lang.String attrValue)
Gets a child element of a node by name and attribute value.

Parameters:
node - Node.
name - Name.
attrName - Attribute name.
attrValue - Attribute value.
Returns:
First child element with given tag name and given attribute value, or null if none found.

getText

public static java.lang.String getText(org.w3c.dom.Node node)
Gets text for a node.

Parameters:
node - Node.
Returns:
Value of first child text node, or the empty string if none found, with leading and trailing white space trimmed.

setText

public static void setText(org.w3c.dom.Node node,
                           java.lang.String text)
Sets text for a node.

Sets the value of the first child text node, if any.

Parameters:
node - Node.
text - New text for the node.

getAllText

public static java.lang.String getAllText(org.w3c.dom.Node node)
Gets all the text for a node.

Parameters:
node - Node.
Returns:
Values of each child text node concateneated together in order, with leading and trailing white space trimmed.

getChildren

public static java.util.ArrayList getChildren(org.w3c.dom.Node node,
                                              java.lang.String name)
Gets the child elements of a node by name.

Parameters:
node - Node.
name - Name.
Returns:
All of the child elements of the node with the given tag name, in order.

getChildren

public static java.util.ArrayList getChildren(org.w3c.dom.Node node,
                                              java.lang.String name,
                                              java.lang.String attrName,
                                              java.lang.String attrValue)
Gets the child elements of a node by name and attribute value.

Parameters:
node - Node.
name - Name.
attrName - Attribute name.
attrValue - Attribute value.
Returns:
All of the child elements of the node with the given tag name which have the given attribute value, in order.

getDescendant

public static org.w3c.dom.Element getDescendant(org.w3c.dom.Node node,
                                                java.lang.String path)
Gets a descendant element of a node.

Parameters:
node - Node.
path - Path to descendant, using tag names of child elements separated by "/".
Returns:
First descendant element, or null if none found.

getDescendants

public static java.util.ArrayList getDescendants(org.w3c.dom.Node node,
                                                 java.lang.String path)
Gets descendant elements of a node.

Parameters:
node - Node.
path - Path to descendants, using tag names of child elements separated by "/".
Returns:
A list of descendant elements.

newDocument

public static org.w3c.dom.Document newDocument()
                                        throws javax.xml.parsers.ParserConfigurationException
Creates a new empty DOM document.

Returns:
New empty DOM document.
Throws:
javax.xml.parsers.ParserConfigurationException

save

public static void save(org.w3c.dom.Document document,
                        java.lang.String path)
                 throws javax.xml.transform.TransformerException,
                        java.io.IOException
Saves a DOM document to an XML file in utf-8.

Parameters:
document - DOM document.
path - Output file path.
Throws:
TransformerException, - IOException
javax.xml.transform.TransformerException
java.io.IOException

nodeHasChild

public static boolean nodeHasChild(org.w3c.dom.Node node,
                                   java.lang.String name)
Checks to see if a node has a child of some name.

Parameters:
node - Node.
name - Name.
Returns:
True if node has a child with the given name.

nodeIsOrHasChild

public static boolean nodeIsOrHasChild(org.w3c.dom.Node node,
                                       java.lang.String name)
Checks to see if a node has a given name or has a child of the given name.

Parameters:
node - Node.
name - Name.
Returns:
True if node has a child with the given name.

nodeHasDescendant

public static boolean nodeHasDescendant(org.w3c.dom.Node node,
                                        java.lang.String name)
Checks to see if a node has a descendant of some name.

Parameters:
node - Node.
name - Name.
Returns:
True if node has a descendant with the given name.

nodeIsOrHasDescendant

public static boolean nodeIsOrHasDescendant(org.w3c.dom.Node node,
                                            java.lang.String name)
Checks to see if a node has a given name or has a descendant of the given name.

Parameters:
node - Node.
name - Name.
Returns:
True if node has or has a descendant with the given name.

nodeHasDescendant

public static boolean nodeHasDescendant(org.w3c.dom.Node node,
                                        java.lang.String[] names1,
                                        java.lang.String[] names2)
Checks to see if a node has a descendant node of some name, other than those in children of some other name.

Parameters:
node - Node
names1 - Array of names to include.
names2 - Array of names to exclude.
Returns:
True if the node contains a descendant in names1, but not in children in names2.