getElementsByTagName
method).
*
* @param ele the DOM element to analyze
* @param childEleNames the child element names to look for
* @return a List of child org.w3c.dom.Element
instances
* @see Element
* @see Element#getElementsByTagName
*/
public static ListgetElementsByTagName
method).
*
* @param ele the DOM element to analyze
* @param childEleName the child element name to look for
* @return a List of child org.w3c.dom.Element
instances
* @see Element
* @see Element#getElementsByTagName
*/
public static Listorg.w3c.dom.Element
instance, or null
if none found
*/
public static Element getChildElementByTagName(Element ele, String childEleName) {
Assert.notNull(ele, "Element must not be null");
Assert.notNull(childEleName, "Element name must not be null");
NodeList nl = ele.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && nodeNameMatch(node, childEleName)) {
return (Element) node;
}
}
return null;
}
/**
* Utility method that returns the first child element value identified by its name.
*
* @param ele the DOM element to analyze
* @param childEleName the child element name to look for
* @return the extracted text value, or null
if no child element found
*/
public static String getChildElementValueByTagName(Element ele, String childEleName) {
Element child = getChildElementByTagName(ele, childEleName);
return (child != null ? getTextValue(child) : null);
}
/**
* Retrieve all child elements of the given DOM element
* @param ele the DOM element to analyze
* @return a List of child org.w3c.dom.Element
instances
*/
public static ListAppends all CharacterData nodes and
* EntityReference nodes into a single String value, excluding Comment nodes.
*
* @see CharacterData
* @see EntityReference
* @see Comment
*/
public static String getTextValue(Element valueEle) {
Assert.notNull(valueEle, "Element must not be null");
StringBuilder sb = new StringBuilder();
NodeList nl = valueEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
sb.append(item.getNodeValue());
}
}
return sb.toString();
}
/**
* Namespace-aware equals comparison. Returns true
if either {@link Node#getLocalName} or {@link
* Node#getNodeName} equals desiredName
, otherwise returns false
.
*/
public static boolean nodeNameEquals(Node node, String desiredName) {
Assert.notNull(node, "Node must not be null");
Assert.notNull(desiredName, "Desired name must not be null");
return nodeNameMatch(node, desiredName);
}
/**
* Returns a SAX ContentHandler
that transforms callback calls to DOM Node
s.
*
* @param node the node to publish events to
* @return the content handler
*/
public static ContentHandler createContentHandler(Node node) {
return new DomContentHandler(node);
}
/** Matches the given node's name and local name against the given desired name. */
private static boolean nodeNameMatch(Node node, String desiredName) {
return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()));
}
/** Matches the given node's name and local name against the given desired names. */
private static boolean nodeNameMatch(Node node, Collection desiredNames) {
return (desiredNames.contains(node.getNodeName()) || desiredNames.contains(node.getLocalName()));
}
}