Here you can find the source of getDescendant(Node node, String path)
Parameter | Description |
---|---|
node | Node. |
path | Path to descendant, using tag names of child elements separated by "/". |
public static Element getDescendant(Node node, String path)
//package com.java2s; /* Please see the license information at the end of this file. */ import java.util.*; import org.w3c.dom.*; public class Main { /** Gets a descendant element of a node. *// ww w .j a va 2 s . com * @param node Node. * * @param path Path to descendant, using tag names of child * elements separated by "/". * * @return Descendant element, or null if none found. */ public static Element getDescendant(Node node, String path) { StringTokenizer tok = new StringTokenizer(path, "/"); while (tok.hasMoreTokens()) { node = getChild(node, tok.nextToken()); if (node == null) return null; } return (Element) node; } /** Gets a child element of a node by name. * * @param node Node. * * @param name Name. * * @return First child element with given tag name, or * null if none found. */ public static Element getChild(Node node, String name) { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (child.getNodeType() != Node.ELEMENT_NODE) continue; if (child.getNodeName().equals(name)) return (Element) child; } return null; } /** Gets a child element of a node by name and attribute value. * * @param node Node. * * @param name Name. * * @param attrName Attribute name. * * @param attrValue Attribute value. * * @return First child element with given tag name and * given attribute value, or null if none found. */ public static Element getChild(Node node, String name, String attrName, String attrValue) { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (child.getNodeType() != Node.ELEMENT_NODE) continue; if (child.getNodeName().equals(name)) { Element el = (Element) child; if (attrValue.equals(el.getAttribute(attrName))) return el; } } return null; } }