fi.iki.hsivonen.xml
Class DOMUtils

java.lang.Object
  extended by fi.iki.hsivonen.xml.DOMUtils

public class DOMUtils
extends Object

A collection of utility methods for working with the DOM.

Author:
hsivonen

Constructor Summary
DOMUtils()
           
 
Method Summary
static Element findBody(Document doc)
           
static Element findElement(Node node, String namespace, String localName)
          Finds the first occurrence of an element in the subtree rooted at node
static Element getElementById(Node node, String id)
          Finds an element of that has an attribute called id which has the given value and is not in a namespace.
static void importChildrenBefore(Node from, Node to, Node ref)
           
static boolean isWhiteSpace(Node node)
          Checks whether a node is a text or CDATA node consisting of white space only.
static String language(Node node)
           
static Locale languageAsLocale(Node node)
           
static Document loadFromUrl(String url)
           
static void main(String[] args)
           
static DomConsumer newDomConsumer()
           
static DocumentBuilder newNonvalidatingDocumentBuilder()
          Instantiates a DocumentBuilder which is non-validating, is namespaces aware, expands entities using the local DTDCatalog, doesn't make arbitrary network connections, coalesces CDATA sections and ignores comments.
static void serialize(Document doc, OutputStream out)
           
static String textContent(Node node)
          Returns the white space-normalized text content of the subtree rooted at node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOMUtils

public DOMUtils()
Method Detail

findElement

public static final Element findElement(Node node,
                                        String namespace,
                                        String localName)
Finds the first occurrence of an element in the subtree rooted at node

Parameters:
node - the root of the subtree to search
namespace - the namespace URI of the element being seached
localName - the local name of the element being seached
Returns:
the first occurrence of the named element or null if not found

getElementById

public static final Element getElementById(Node node,
                                           String id)
Finds an element of that has an attribute called id which has the given value and is not in a namespace. The IDness of the attribute is based on the attribute name--not on the DTD.

Parameters:
node - the root of the subtree to search
id - the value of the id attribute
Returns:
the first element that has the specified attribute

textContent

public static final String textContent(Node node)
Returns the white space-normalized text content of the subtree rooted at node.

Parameters:
node - the subtree
Returns:
the white space-normalized text content

newNonvalidatingDocumentBuilder

public static final DocumentBuilder newNonvalidatingDocumentBuilder()
Instantiates a DocumentBuilder which is non-validating, is namespaces aware, expands entities using the local DTDCatalog, doesn't make arbitrary network connections, coalesces CDATA sections and ignores comments.

Returns:
a configured DocumentBuilder

isWhiteSpace

public static final boolean isWhiteSpace(Node node)
Checks whether a node is a text or CDATA node consisting of white space only.

Parameters:
node - the node to examine
Returns:
true if it is a white space node and false otherwise

language

public static final String language(Node node)

languageAsLocale

public static final Locale languageAsLocale(Node node)

findBody

public static final Element findBody(Document doc)

importChildrenBefore

public static final void importChildrenBefore(Node from,
                                              Node to,
                                              Node ref)

newDomConsumer

public static DomConsumer newDomConsumer()

serialize

public static void serialize(Document doc,
                             OutputStream out)
                      throws IOException
Throws:
IOException

loadFromUrl

public static Document loadFromUrl(String url)
                            throws SAXException,
                                   IOException
Throws:
SAXException
IOException

main

public static void main(String[] args)
                 throws SAXException,
                        IOException
Throws:
SAXException
IOException