org.yajul.xml
Class DOMUtil

java.lang.Object
  extended by org.yajul.xml.DOMUtil

public class DOMUtil
extends java.lang.Object

Provides commonly used DOM operations in convenient methods. Good for replacing many lines of DOM code with a nice one-liner. Works with any JAXP 1.1 implementation (e.g. XERCES/XALAN).

Author:
Joshua Davis

Constructor Summary
DOMUtil()
           
 
Method Summary
static org.w3c.dom.Element addChild(org.w3c.dom.Document document, org.w3c.dom.Element parent, java.lang.String childTag)
          Adds a child element to the specified parent element.
static org.w3c.dom.Element addChildWithText(org.w3c.dom.Document document, org.w3c.dom.Element parent, java.lang.String childTag, java.lang.String text)
          Adds a child element to the specified parent element containing some text.
static org.w3c.dom.Document createDocument()
          Creates a new DOM document with no root element.
static org.w3c.dom.Document createDocument(java.lang.String rootElementTag)
          Creates a new DOM document with the specified root element as the 'document element'.
static java.util.Map<java.lang.String,java.lang.String> getAttributeMap(org.w3c.dom.Element elem)
          Returns a map of all the attributes in the element.
static java.util.List<java.lang.String> getAttributeNames(org.w3c.dom.Element elem)
          Returns the names of all the attributes in the element as an array of strings.
static boolean getBooleanAttribute(org.w3c.dom.Element elem, java.lang.String attributeName, boolean defaultValue)
          Returns the value of the specified attribute as a boolean primitive.
static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Document parent)
          Returns a list of child elements.
static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Document parent, java.lang.String tag)
          Returns an array of child elements with the specified tag name.
static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parent)
          Returns the list of child elements in a parent element
static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parent, java.lang.String tag)
          Returns a list of ALL child elements in this element (either directly or inside a descendant) with the specified tag name.
static java.lang.String getChildText(org.w3c.dom.Element element)
          Returns the text inside an element as a string.
static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
          Returns a new document builder.
static int getIntAttribute(org.w3c.dom.Element elem, java.lang.String attributeName, int defaultValue)
          Returns the value of the specified attribute as an int primitive.
static org.w3c.dom.Document parse(java.io.InputStream input)
          Parses the input stream and returns a DOM document.
static org.w3c.dom.Document parse(org.xml.sax.XMLReader reader, org.xml.sax.InputSource input)
          Parse the input with the specified reader, producing a DOM Document.
static org.w3c.dom.Document parseFile(java.io.File file)
          Parse an XML file.
static org.w3c.dom.Document parseFile(java.lang.String fileName)
          Parse a file using it's name.
static org.w3c.dom.Document parseResource(java.lang.String resourceName)
          Parse an XML resource.
static java.util.List<org.w3c.dom.Element> toElementList(org.w3c.dom.NodeList nodeList)
          Returns an array of elements, given a node list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOMUtil

public DOMUtil()
Method Detail

createDocument

public static org.w3c.dom.Document createDocument(java.lang.String rootElementTag)
                                           throws javax.xml.parsers.ParserConfigurationException
Creates a new DOM document with the specified root element as the 'document element'.

Parameters:
rootElementTag - The tag name for the root element.
Returns:
Document - The new document.
Throws:
javax.xml.parsers.ParserConfigurationException - - If the document could not be created.

createDocument

public static org.w3c.dom.Document createDocument()
                                           throws javax.xml.parsers.ParserConfigurationException
Creates a new DOM document with no root element.

Returns:
Document - The new document.
Throws:
javax.xml.parsers.ParserConfigurationException - - If the document could not be created.

addChild

public static org.w3c.dom.Element addChild(org.w3c.dom.Document document,
                                           org.w3c.dom.Element parent,
                                           java.lang.String childTag)
Adds a child element to the specified parent element.

Parameters:
document - The document that contains the parent.
parent - The parent element.
childTag - The tag name for the new child.
Returns:
Element - The new child element.

addChildWithText

public static org.w3c.dom.Element addChildWithText(org.w3c.dom.Document document,
                                                   org.w3c.dom.Element parent,
                                                   java.lang.String childTag,
                                                   java.lang.String text)
Adds a child element to the specified parent element containing some text.

Parameters:
document - The document that contains the parent.
parent - The parent element.
childTag - The tag name for the new child.
text - The text that will be inside the new child. Note: If 'text' is null, a zero length string will be used in order to avoid NPE's (in XALAN2) later on.
Returns:
Element - The new child element.

getDocumentBuilder

public static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
                                                            throws javax.xml.parsers.ParserConfigurationException
Returns a new document builder.

Returns:
DocumentBuilder - A new document builder.
Throws:
javax.xml.parsers.ParserConfigurationException - - If the JAXP implementation is configured incorrectly

toElementList

public static java.util.List<org.w3c.dom.Element> toElementList(org.w3c.dom.NodeList nodeList)
Returns an array of elements, given a node list.

Parameters:
nodeList - The node list.
Returns:
a list of elements

getChildElements

public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parent)
Returns the list of child elements in a parent element

Parameters:
parent - The parent element.
Returns:
the list of elements in the parent

getChildElements

public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Document parent)
Returns a list of child elements.

Parameters:
parent - The parent document.
Returns:
the list of elements in the parent

getChildElements

public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parent,
                                                                   java.lang.String tag)
Returns a list of ALL child elements in this element (either directly or inside a descendant) with the specified tag name.

Parameters:
parent - The parent element.
tag - The child element tag name.
Returns:
The list of ALL elements in parent that have the specified tag name.

getChildElements

public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Document parent,
                                                                   java.lang.String tag)
Returns an array of child elements with the specified tag name.

Parameters:
parent - The parent element.
tag - - The child element tag name.
Returns:
The list of ALL elements in parent that have the specified tag name.

getChildText

public static java.lang.String getChildText(org.w3c.dom.Element element)
Returns the text inside an element as a string. If there are multiple text nodes, they will be concantenated.

Parameters:
element - The element containing the text.
Returns:
String - The text in the element.

getAttributeNames

public static java.util.List<java.lang.String> getAttributeNames(org.w3c.dom.Element elem)
Returns the names of all the attributes in the element as an array of strings.

Parameters:
elem - The element.
Returns:
the names of all the attributes in the element as an array of strings.

getAttributeMap

public static java.util.Map<java.lang.String,java.lang.String> getAttributeMap(org.w3c.dom.Element elem)
Returns a map of all the attributes in the element. The keys will be the attribute names and the values will be the attribute values.

Parameters:
elem - The element.
Returns:
a map of all the attributes in the element. The keys will be the attribute names and the values will be the attribute values.

getBooleanAttribute

public static boolean getBooleanAttribute(org.w3c.dom.Element elem,
                                          java.lang.String attributeName,
                                          boolean defaultValue)
Returns the value of the specified attribute as a boolean primitive.

Parameters:
elem - The element.
attributeName - The attribute name in the element.
defaultValue - The default value.
Returns:
the value of the specified attribute as a boolean primitive.

getIntAttribute

public static int getIntAttribute(org.w3c.dom.Element elem,
                                  java.lang.String attributeName,
                                  int defaultValue)
Returns the value of the specified attribute as an int primitive.

Parameters:
elem - The element.
attributeName - The attribute name in the element.
defaultValue - The default value.
Returns:
the value of the specified attribute as an int primitive.

parse

public static org.w3c.dom.Document parse(java.io.InputStream input)
                                  throws javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException,
                                         java.io.IOException
Parses the input stream and returns a DOM document.

Parameters:
input - - The input stream to parse.
Returns:
Document - The DOM Document.
Throws:
javax.xml.parsers.ParserConfigurationException - - If the JAXP implementation is configured incorrectly
org.xml.sax.SAXException - - If the document could not be parsed
java.io.IOException - - If there was something wrong with the input stream.

parseFile

public static org.w3c.dom.Document parseFile(java.lang.String fileName)
                                      throws javax.xml.parsers.ParserConfigurationException,
                                             org.xml.sax.SAXException,
                                             java.io.IOException
Parse a file using it's name.

Parameters:
fileName - The name of the file to parse.
Returns:
A DOM document.
Throws:
javax.xml.parsers.ParserConfigurationException - - If the JAXP implementation is configured incorrectly
org.xml.sax.SAXException - - If the document could not be parsed
java.io.IOException - - If there was something wrong with the input stream.

parseFile

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

Parameters:
file - The file to parse.
Returns:
A DOM document.
Throws:
javax.xml.parsers.ParserConfigurationException - - If the JAXP implementation is configured incorrectly
org.xml.sax.SAXException - - If the document could not be parsed
java.io.IOException - - If there was something wrong with the input stream.

parseResource

public static org.w3c.dom.Document parseResource(java.lang.String resourceName)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException,
                                                 java.io.IOException
Parse an XML resource.

Parameters:
resourceName - The resource to parse.
Returns:
A DOM document.
Throws:
javax.xml.parsers.ParserConfigurationException - If the JAXP implementation is configured incorrectly
org.xml.sax.SAXException - If the document could not be parsed
java.io.IOException - If there was something wrong with the input stream.

parse

public static org.w3c.dom.Document parse(org.xml.sax.XMLReader reader,
                                         org.xml.sax.InputSource input)
                                  throws javax.xml.transform.TransformerConfigurationException,
                                         java.io.IOException,
                                         org.xml.sax.SAXException
Parse the input with the specified reader, producing a DOM Document.

Parameters:
reader - The reader, which will produce SAX2 events.
input - The input source
Returns:
The DOM document.
Throws:
javax.xml.transform.TransformerConfigurationException - if the transformer doesn't support this operation.
java.io.IOException - if the input cannot be read
org.xml.sax.SAXException - if the input cannot be parsed


Copyright © 2008. All Rights Reserved.