com.javaranch.common
Class DOM

java.lang.Object
  |
  +--com.javaranch.common.DOM

public class DOM
extends java.lang.Object

Some static method shortcuts to help with working with DOM.

- - - - - - - - - - - - - - - - -

Copyright (c) 1999-2004 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton

Constructor Summary
DOM()
           
 
Method Summary
static java.lang.String getAllText(org.w3c.dom.Element node)
          Given an element, find all of the text children and concatenate them together.
static boolean getBooleanAttribute(org.w3c.dom.Element node, java.lang.String attributeName, java.lang.String trueText, boolean defaultValue)
          Get the boolean representation of the attribute.
static org.w3c.dom.NodeList getChildren(org.w3c.dom.Node node, java.lang.String tagName)
          Retreives all of the children that have this particular tag name.
static java.lang.String getChildText(org.w3c.dom.Element node, java.lang.String childName)
          Find the child element and extract it's text with getText().
static int getIntAttribute(org.w3c.dom.Element node, java.lang.String attributeName)
          Get the int representation of the attribute.
static org.w3c.dom.Element getRoot(java.lang.String xml)
          Pass in an XML string and get back the root DOM element.
static java.lang.String getText(org.w3c.dom.Element node)
          given an element, extract the text.
static java.lang.String getTextAttribute(org.w3c.dom.Element node, java.lang.String attributeName)
          Get the String representation of the attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOM

public DOM()
Method Detail

getTextAttribute

public static java.lang.String getTextAttribute(org.w3c.dom.Element node,
                                                java.lang.String attributeName)
Get the String representation of the attribute.

Returns:
never returns null. Empty string if no match.


getIntAttribute

public static int getIntAttribute(org.w3c.dom.Element node,
                                  java.lang.String attributeName)
                           throws java.lang.Exception
Get the int representation of the attribute.

Throws an exception if there is no match.

java.lang.Exception

getBooleanAttribute

public static boolean getBooleanAttribute(org.w3c.dom.Element node,
                                          java.lang.String attributeName,
                                          java.lang.String trueText,
                                          boolean defaultValue)
Get the boolean representation of the attribute.

Parameters:
trueText - If the text in DOM matches this string, true will be returned.

defaultValue - If the attribute is not found, this is what will be returned.


getText

public static java.lang.String getText(org.w3c.dom.Element node)
given an element, extract the text.

Useful only if you know there is nothing between the node tags other than text. If there are other tags, this will return whatever text appears before the first tag.


getAllText

public static java.lang.String getAllText(org.w3c.dom.Element node)
Given an element, find all of the text children and concatenate them together.

All leading and trailing whitespace will be removed.


getChildText

public static java.lang.String getChildText(org.w3c.dom.Element node,
                                            java.lang.String childName)
                                     throws java.lang.Exception
Find the child element and extract it's text with getText().

java.lang.Exception

getChildren

public static org.w3c.dom.NodeList getChildren(org.w3c.dom.Node node,
                                               java.lang.String tagName)
Retreives all of the children that have this particular tag name.


getRoot

public static org.w3c.dom.Element getRoot(java.lang.String xml)
                                   throws java.lang.Exception
Pass in an XML string and get back the root DOM element.

java.lang.Exception


Copyright ©2004 Paul Wheaton All Rights Reserved