Java XML Node Path extractPath(Node node, String[] path)

Here you can find the source of extractPath(Node node, String[] path)

Description

Returns first node at the bottom of path from node.

License

Apache License

Parameter

Parameter Description
node Node to apply path to
path Path to apply

Return

Node at bottom of path, or null

Declaration

static public Node extractPath(Node node, String[] path) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//  www .  ja va 2  s  . co m
     *  Returns first node at the bottom of path from node.
     * If element begins with '@', indicates an attribute, eg "@id"
     * The '#text' element indicates that the node has a single text child.
     * @param node    Node to apply path to
     * @param path    Path to apply
     * @return        Node at bottom of path, or null
     */
    static public Node extractPath(Node node, String[] path) {
        for (int i = 0; i < path.length; i++)
            node = extractNode(node, path[i]);
        return node;
    }

    /**
     *  Returns first node at the bottom of path from node.
     * If element begins with '@', indicates an attribute, eg "@id"
     * The '#text' element indicates that the node has a single text child.
     * @param node    Node to apply path to
     * @param path    Path to apply
     * @return        Node at bottom of path, or null
     */
    static public Node extractNode(Node node, String path) {
        if (node == null)
            return null;
        NodeList list = node.getChildNodes();
        if (path.equals("#text"))
            return node.getFirstChild();
        else if (path.charAt(0) == '@')
            return node.getAttributes().getNamedItem(path.substring(1));
        else
            for (int j = 0; j < list.getLength(); j++)
                if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                    return list.item(j);

        return null;
    }
}

Related

  1. addElementPath(Node element, String path)
  2. createAndAppendNode(Node root, String path)
  3. extractNodes(Node node, String path)
  4. extractPaths(Node node, String[] path)
  5. getCompletePathForANode(Node objNode)
  6. getContent(Node n, String path)
  7. getDescendant(Node node, String path)