Top Description Constructors Methods
org.springframework.util.xml

public abstract Class DomUtils

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .Collection, .List, org.w3c.dom.CharacterData, .Comment, .Element, .EntityReference, .Node, .NodeList, org.xml.sax.ContentHandler, org.springframework.lang.Nullable, org.springframework.util.Assert

Convenience methods for working with the DOM API, in particular for working with DOM Nodes and DOM Elements.
Authors
Juergen Hoeller, Rob Harrop, Costin Leau, Arjen Poutsma, Luke Taylor
Since
1.2
See Also
org.w3c.dom.Node, org.w3c.dom.Element

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static ContentHandler

Returns:

the content handler
createContentHandler
(Node
the node to publish events to
node
)

Returns a SAX ContentHandler that transforms callback calls to DOM Nodes.

public static Element

Returns:

the org.w3c.dom.Element instance, or null if none found
getChildElementByTagName
(Element
the DOM element to analyze
ele
,
String
the child element name to look for
childEleName
)

Utility method that returns the first child element identified by its name.

public static List<Element>

Returns:

a List of child org.w3c.dom.Element instances
getChildElements
(Element
the DOM element to analyze
ele
)

Retrieves all child elements of the given DOM element.

public static List<Element>

Returns:

a List of child org.w3c.dom.Element instances
getChildElementsByTagName
(Element
the DOM element to analyze
ele
,
String...
the child element names to look for
childEleNames
)

Retrieves all child elements of the given DOM element that match any of the given element names.

public static List<Element>

Returns:

a List of child org.w3c.dom.Element instances
getChildElementsByTagName
(Element
the DOM element to analyze
ele
,
String
the child element name to look for
childEleName
)

Retrieves all child elements of the given DOM element that match the given element name.

public static String

Returns:

the extracted text value, or null if no child element found
getChildElementValueByTagName
(Element
the DOM element to analyze
ele
,
String
the child element name to look for
childEleName
)

Utility method that returns the first child element value identified by its name.

public static String
getTextValue(Element valueEle)

Extracts the text value from the given DOM element, ignoring XML comments.

public static boolean
nodeNameEquals(Node node, String desiredName)

Namespace-aware equals comparison.

private static boolean
nodeNameMatch(Node node, String desiredName)

Matches the given node's name and local name against the given desired name.

private static boolean
nodeNameMatch(Node node, Collection<?> desiredNames)

Matches the given node's name and local name against the given desired names.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DomUtilsback to summary
public DomUtils()

Method Detail

createContentHandlerback to summary
public static ContentHandler createContentHandler(Node node)

Returns a SAX ContentHandler that transforms callback calls to DOM Nodes.

Parameters
node:Node

the node to publish events to

Returns:ContentHandler

the content handler

getChildElementByTagNameback to summary
public static Element getChildElementByTagName(Element ele, String childEleName)

Utility method that returns the first child element identified by its name.

Parameters
ele:Element

the DOM element to analyze

childEleName:String

the child element name to look for

Returns:Element

the org.w3c.dom.Element instance, or null if none found

Annotations
@Nullable
getChildElementsback to summary
public static List<Element> getChildElements(Element ele)

Retrieves all child elements of the given DOM element.

Parameters
ele:Element

the DOM element to analyze

Returns:List<Element>

a List of child org.w3c.dom.Element instances

getChildElementsByTagNameback to summary
public static List<Element> getChildElementsByTagName(Element ele, String... childEleNames)

Retrieves all child elements of the given DOM element that match any of the given element names. Only looks at the direct child level of the given element; do not go into further depth (in contrast to the DOM API's getElementsByTagName method).

Parameters
ele:Element

the DOM element to analyze

childEleNames:String[]

the child element names to look for

Returns:List<Element>

a List of child org.w3c.dom.Element instances

See Also
org.w3c.dom.Element, org.w3c.dom.Element#getElementsByTagName
getChildElementsByTagNameback to summary
public static List<Element> getChildElementsByTagName(Element ele, String childEleName)

Retrieves all child elements of the given DOM element that match the given element name. Only look at the direct child level of the given element; do not go into further depth (in contrast to the DOM API's getElementsByTagName method).

Parameters
ele:Element

the DOM element to analyze

childEleName:String

the child element name to look for

Returns:List<Element>

a List of child org.w3c.dom.Element instances

See Also
org.w3c.dom.Element, org.w3c.dom.Element#getElementsByTagName
getChildElementValueByTagNameback to summary
public static String getChildElementValueByTagName(Element ele, String childEleName)

Utility method that returns the first child element value identified by its name.

Parameters
ele:Element

the DOM element to analyze

childEleName:String

the child element name to look for

Returns:String

the extracted text value, or null if no child element found

Annotations
@Nullable
getTextValueback to summary
public static String getTextValue(Element valueEle)

Extracts the text value from the given DOM element, ignoring XML comments.

Appends all CharacterData nodes and EntityReference nodes into a single String value, excluding Comment nodes. Only exposes actual user-specified text, no default values of any kind.

See Also
CharacterData, EntityReference, Comment
nodeNameEqualsback to summary
public static boolean nodeNameEquals(Node node, String desiredName)

Namespace-aware equals comparison. Returns true if either Node#getLocalName or Node#getNodeName equals desiredName, otherwise returns false.

nodeNameMatchback to summary
private static boolean nodeNameMatch(Node node, String desiredName)

Matches the given node's name and local name against the given desired name.

nodeNameMatchback to summary
private static boolean nodeNameMatch(Node node, Collection<?> desiredNames)

Matches the given node's name and local name against the given desired names.