com.edustructures.util
Class XMLUtils

java.lang.Object
  extended by com.edustructures.util.XMLUtils

public class XMLUtils
extends java.lang.Object

Various static helper routines for manipulating DOM Documents.


Constructor Summary
XMLUtils()
           
 
Method Summary
static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String attr)
          Gets a named attribute of a Node
static java.lang.String getElementAttribute(org.w3c.dom.Node node, java.lang.String element, java.lang.String attribute, java.lang.String defaultValue)
          A convenience function to get the named attribute of a child element.
static org.w3c.dom.Node getElementByAttr(org.w3c.dom.Node node, java.lang.String element, java.lang.String attribute, java.lang.String value)
          Gets the first child element with the specified tag name having an attribute set to the specified value.
static java.util.List<org.w3c.dom.Node> getElementsByTagName(org.w3c.dom.Node node, java.lang.String element, boolean filter)
          Gets all child elements with the specified tag name, optionally excluding those elements that have an enabled attribute set to false.
static java.lang.String getElementTextValue(org.w3c.dom.Document doc, java.lang.String element)
          Returns the text value of the first element in the document with the specified tag name
static java.lang.String getElementTextValue(org.w3c.dom.Node node, java.lang.String element)
          Returns the text value of the first child element found with the specified tag name.
static org.w3c.dom.Node getFirstElementIgnoreCase(org.w3c.dom.Node node, java.lang.String element)
          Returns the first child element found with the specified tag name
static org.w3c.dom.Node getFirstElementWithTagName(org.w3c.dom.Node node, java.lang.String element)
          Returns the first child element found with the specified tag name
static java.lang.String getText(org.w3c.dom.Node n)
          Gets the text for a node by concatenating child TEXT elements.
static void removeAttribute(org.w3c.dom.Node node, java.lang.String attr)
          Removes a named attribute of a Node
static void setAttribute(org.w3c.dom.Node node, java.lang.String attr, java.lang.String value)
          Sets a named attribute of a Node
static void setElementAttribute(org.w3c.dom.Node node, java.lang.String element, java.lang.String attribute, java.lang.String value)
          A convenience function to set the named attribute of a child element.
static void setOrRemoveAttribute(org.w3c.dom.Element element, java.lang.String attr, java.lang.String value)
          Sets a named attribute of an Element, or removes the attribute if the value is null;
static void setOrRemoveAttribute(org.w3c.dom.Node node, java.lang.String attr, java.lang.String value)
          Sets a named attribute of a Node, or removes the attribute if the value is null;
static void setText(org.w3c.dom.Node n, java.lang.String text)
          Sets the text for a node
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

getText

public static java.lang.String getText(org.w3c.dom.Node n)
Gets the text for a node by concatenating child TEXT elements.


setText

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


getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Node node,
                                            java.lang.String attr)
Gets a named attribute of a Node

Parameters:
node - The node to search
attr - The name of the attribute to get

setAttribute

public static void setAttribute(org.w3c.dom.Node node,
                                java.lang.String attr,
                                java.lang.String value)
Sets a named attribute of a Node

Parameters:
node - The node
attr - The name of the attribute to set
value - The value to assign to the attribute

setOrRemoveAttribute

public static void setOrRemoveAttribute(org.w3c.dom.Node node,
                                        java.lang.String attr,
                                        java.lang.String value)
Sets a named attribute of a Node, or removes the attribute if the value is null;

Parameters:
node - The node to search
attr - The name of the attribute to set or remove
value - The value to assign, or NULL to remove

setOrRemoveAttribute

public static void setOrRemoveAttribute(org.w3c.dom.Element element,
                                        java.lang.String attr,
                                        java.lang.String value)
Sets a named attribute of an Element, or removes the attribute if the value is null;

Parameters:
element - The element to search
attr - The name of the attribute to set or remove
value - The value to assign, or NULL to remove

removeAttribute

public static void removeAttribute(org.w3c.dom.Node node,
                                   java.lang.String attr)
Removes a named attribute of a Node

Parameters:
node - The node to search
attr - The name of the attribute to remove

getElementTextValue

public static java.lang.String getElementTextValue(org.w3c.dom.Node node,
                                                   java.lang.String element)
Returns the text value of the first child element found with the specified tag name.

Parameters:
node - The node to search
element - The element tag name to search for
Returns:
The value of the element if found

getElementTextValue

public static java.lang.String getElementTextValue(org.w3c.dom.Document doc,
                                                   java.lang.String element)
Returns the text value of the first element in the document with the specified tag name

Parameters:
doc - The Document to search
element - The element tag name to search for
Returns:
The value of the element if found

getFirstElementWithTagName

public static org.w3c.dom.Node getFirstElementWithTagName(org.w3c.dom.Node node,
                                                          java.lang.String element)
Returns the first child element found with the specified tag name

Parameters:
node - The node to search
element - The element tag name to search for
Returns:
The matching element Node

getFirstElementIgnoreCase

public static org.w3c.dom.Node getFirstElementIgnoreCase(org.w3c.dom.Node node,
                                                         java.lang.String element)
Returns the first child element found with the specified tag name

Parameters:
node - The node to search
element - The element tag name to search for
Returns:
The matching element Node

getElementsByTagName

public static java.util.List<org.w3c.dom.Node> getElementsByTagName(org.w3c.dom.Node node,
                                                                    java.lang.String element,
                                                                    boolean filter)
Gets all child elements with the specified tag name, optionally excluding those elements that have an enabled attribute set to false.

Parameters:
node - The parent node
element - The element tag name
filter - Do not return elements that have the enabled attribute set to false
Returns:
A Vector of Node objects

getElementByAttr

public static org.w3c.dom.Node getElementByAttr(org.w3c.dom.Node node,
                                                java.lang.String element,
                                                java.lang.String attribute,
                                                java.lang.String value)
Gets the first child element with the specified tag name having an attribute set to the specified value.

Parameters:
node - The parent node
element - The element tag name
attribute - The attribute name
value - The attribute value to match
Returns:
The Node that matches the criteria, or null if no such element is found as a child of node

getElementAttribute

public static java.lang.String getElementAttribute(org.w3c.dom.Node node,
                                                   java.lang.String element,
                                                   java.lang.String attribute,
                                                   java.lang.String defaultValue)
A convenience function to get the named attribute of a child element.

This method searches the specified Node for the first matching element, then returns the value of the named attribute. If the attribute does not exist the default value is returned.

Parameters:
node - The node to search
element - The tag name of the element to search (it must be a non- repeating child of the node)
attribute - The name of the attribute
defaultValue - The value to return if the property is not found
Returns:
The attribute value

setElementAttribute

public static void setElementAttribute(org.w3c.dom.Node node,
                                       java.lang.String element,
                                       java.lang.String attribute,
                                       java.lang.String value)
A convenience function to set the named attribute of a child element.

This method searches the specified Node for the first matching element, then sets the value of the named attribute. If the element does not exist, no action is taken.

Parameters:
node - The node to search
element - The tag name of the element to search (it must be a non- repeating child of the node)
attribute - The name of the attribute
value - The value of the attribute


Copyright © 2001-2007 Edustructures LLC. All Rights Reserved. SIFWorks® and ADK® are registered trademarks of Edustructures LLC. SIF™ and Schools Interoperability Framework are trademarks of the Schools Interoperability Framework Association.