org.backsource.utils.xml
Class ElementUtil

java.lang.Object
  extended byorg.backsource.utils.xml.ElementUtil

public class ElementUtil
extends java.lang.Object

Utility methods to help working with Elements.

To work in Nodes instead, see DocumentUtil

Version:
$Revision: 1.1.1.1 $
Author:
Peter Antman

Method Summary
static java.lang.String getAttribute(org.w3c.dom.Element element, java.lang.String name)
          Get attribute with name name from element, only return a non null value if the attribute was not null and not an empty string.
static java.lang.String getAttribute(org.w3c.dom.Element element, java.lang.String name, java.lang.String defaultValue)
          Get attribute with name name from element, only return a non null value if the attribute was not null and not an empty string, otherwise return defaultValue.
static boolean getAttributeAsBoolean(org.w3c.dom.Element e, java.lang.String name)
          Get the an attribute as a boolean.
static float getAttributeAsFloat(org.w3c.dom.Element e, java.lang.String name)
          Get the an attribute as a float.
static int getAttributeAsInt(org.w3c.dom.Element e, java.lang.String name)
          Get the an attribute as an int
static long getAttributeAsLong(org.w3c.dom.Element e, java.lang.String name)
          Get the an attribute as a long
static java.lang.String getContent(org.w3c.dom.Element element)
          Get the string content of the element.
static java.lang.String getContent(org.w3c.dom.Element element, java.lang.String defaultStr)
          Get the string content of the element.
static java.util.List getElements(org.w3c.dom.Element element)
          Get all element children of element.
static java.util.List getElementsByTagName(org.w3c.dom.Element element, java.lang.String tagName)
          Get all element children of element whitch match tagName
static org.w3c.dom.Element getOptionalChild(org.w3c.dom.Element element, java.lang.String tagName)
          Gets the child of the specified element having the specified name.
static org.w3c.dom.Element getOptionalChild(org.w3c.dom.Element element, java.lang.String tagName, org.w3c.dom.Element defaultElement)
          Gets the child of the specified element having the specified name.
static org.w3c.dom.Element getUniqueChild(org.w3c.dom.Element element, java.lang.String tagName)
          Gets the child of the specified element having the specified unique name.
static void main(java.lang.String[] args)
           
static void setContent(org.w3c.dom.Element element, java.lang.String content)
          Set the text content of this element.
static void setElement(org.w3c.dom.Element element, org.w3c.dom.DocumentFragment content)
          Set DocumentFragment as a child element, removing all old elements.
static void setElement(org.w3c.dom.Element element, org.w3c.dom.Element content)
          Set element as a child element, removing all old elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getElements

public static java.util.List getElements(org.w3c.dom.Element element)
                                  throws XmlException
Get all element children of element.

Throws:
XmlException

getElementsByTagName

public static java.util.List getElementsByTagName(org.w3c.dom.Element element,
                                                  java.lang.String tagName)
                                           throws XmlException
Get all element children of element whitch match tagName

Throws:
XmlException

getOptionalChild

public static org.w3c.dom.Element getOptionalChild(org.w3c.dom.Element element,
                                                   java.lang.String tagName)
                                            throws XmlException
Gets the child of the specified element having the specified name. If the child with this name doesn't exist then null is returned instead.

Parameters:
element - the parent element
tagName - the name of the desired child
Returns:
either the named child or null
Throws:
XmlException

getOptionalChild

public static org.w3c.dom.Element getOptionalChild(org.w3c.dom.Element element,
                                                   java.lang.String tagName,
                                                   org.w3c.dom.Element defaultElement)
                                            throws XmlException
Gets the child of the specified element having the specified name. If the child with this name doesn't exist then the supplied default element is returned instead.

Parameters:
element - the parent element
tagName - the name of the desired child
defaultElement - the element to return if the child doesn't exist
Returns:
either the named child or the supplied default
Throws:
XmlException

getUniqueChild

public static org.w3c.dom.Element getUniqueChild(org.w3c.dom.Element element,
                                                 java.lang.String tagName)
                                          throws XmlException
Gets the child of the specified element having the specified unique name. If there are more than one children elements with the same name and exception is thrown.

Parameters:
element - The parent element
tagName - The name of the desired child
Returns:
The named child.
Throws:
XmlException - Child was not found or was not unique.

getContent

public static java.lang.String getContent(org.w3c.dom.Element element)
Get the string content of the element. Merging all textnodes and cdata nodes.


getContent

public static java.lang.String getContent(org.w3c.dom.Element element,
                                          java.lang.String defaultStr)
Get the string content of the element. Merging all textnodes and cdata nodes.


setContent

public static void setContent(org.w3c.dom.Element element,
                              java.lang.String content)
                       throws XmlException
Set the text content of this element. Owerwriting any prevous nodes.

Throws:
XmlException

setElement

public static void setElement(org.w3c.dom.Element element,
                              org.w3c.dom.Element content)
                       throws XmlException
Set element as a child element, removing all old elements.

Throws:
XmlException

setElement

public static void setElement(org.w3c.dom.Element element,
                              org.w3c.dom.DocumentFragment content)
                       throws XmlException
Set DocumentFragment as a child element, removing all old elements.

Throws:
XmlException

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element element,
                                            java.lang.String name)
                                     throws XmlException
Get attribute with name name from element, only return a non null value if the attribute was not null and not an empty string.

Returns:
null if attribute as null or an empty string.
Throws:
XmlException - if element or name is null.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element element,
                                            java.lang.String name,
                                            java.lang.String defaultValue)
                                     throws XmlException
Get attribute with name name from element, only return a non null value if the attribute was not null and not an empty string, otherwise return defaultValue.

Returns:
default value if attribute as null or an empty string.
Throws:
XmlException - if element or name is null.

getAttributeAsInt

public static int getAttributeAsInt(org.w3c.dom.Element e,
                                    java.lang.String name)
                             throws XmlException
Get the an attribute as an int

Throws:
XmlException - if the attribute could not be parsed.

getAttributeAsLong

public static long getAttributeAsLong(org.w3c.dom.Element e,
                                      java.lang.String name)
                               throws XmlException
Get the an attribute as a long

Throws:
XmlException - if the attribute could not be parsed.

getAttributeAsBoolean

public static boolean getAttributeAsBoolean(org.w3c.dom.Element e,
                                            java.lang.String name)
                                     throws XmlException
Get the an attribute as a boolean. Handles null values correctly: No fall back on false, as (new Boolean(value)).booleanValue() does.

Throws:
XmlException - if the attribute could not be parsed.

getAttributeAsFloat

public static float getAttributeAsFloat(org.w3c.dom.Element e,
                                        java.lang.String name)
                                 throws XmlException
Get the an attribute as a float.

Throws:
XmlException - if the attribute could not be parsed.

main

public static void main(java.lang.String[] args)


Copyright © 2000-2004 Backsource. All Rights Reserved.