Java XML Node Path getDescendant(Node node, String path)

Here you can find the source of getDescendant(Node node, String path)

Description

Gets a descendant element of a node.

License

Open Source License

Parameter

Parameter Description
node Node.
path Path to descendant, using tag names of child elements separated by "/".

Return

Descendant element, or null if none found.

Declaration


public static Element getDescendant(Node node, String path) 

Method Source Code


//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;
    }
}

Related

  1. extractNodes(Node node, String path)
  2. extractPath(Node node, String[] path)
  3. extractPaths(Node node, String[] path)
  4. getCompletePathForANode(Node objNode)
  5. getContent(Node n, String path)
  6. getElementViaPath(Node node, String path)
  7. getFullPath(Node node)
  8. getIndividualPath(Node individual)
  9. getMatchingNodes(Node node, String[] nodePath, int cur, List res)