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

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

Description

Get the value of a node specified by a starting node and a path string.

License

Open Source License

Parameter

Parameter Description
node the top of the tree to search.
path the path from the top of the tree to the desired node.

Return

the value of the first node matching the path, or the empty string if no node exists at the path location or if the starting node is not an element.

Declaration

public static String getValueViaPath(Node node, String path) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class Main {
    /**/*from   w  w  w .ja  v a2 s .  c  o m*/
     * Get the value of a node specified by a starting node and a
     * path string. If the starting node is a Document, use the
     * document element as the starting point.
     * <ul>
     * <li>A path to an element has the form: elem1/.../elemN
     * <li>A path to an attribute has the form: elem1/.../elemN@attr
     * </ul>
     * The value of an element node is the sum of all the element's
     * first generation child text nodes. Note that this is not what you
     * would get from a mixed element in an XSL program.
     * @param node the top of the tree to search.
     * @param path the path from the top of the tree to the desired node.
     * @return the value of the first node matching the path, or the
     * empty string if no node exists at the path location or if the
     * starting node is not an element.
     */
    public static String getValueViaPath(Node node, String path) {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();
        if (!(node instanceof Element))
            return "";
        path = path.trim();
        int kAtsign = path.indexOf("@");

        //If the target is an element, get the element's value.
        if (kAtsign == -1) {
            Element target = getElementViaPath(node, path);
            if (target == null)
                return "";
            return target.getTextContent();
        }

        //The target is an attribute; first find the element.
        String subpath = path.substring(0, kAtsign);
        Element target = getElementViaPath(node, subpath);
        if (target == null)
            return null;
        String name = path.substring(kAtsign + 1);
        return target.getAttribute(name);
    }

    /**
     * Get an element specified by a starting node and a path string.
     * If the starting node is a Document, use the document element
     * as the starting point.
     * <ul><li>A path to an element has the form: elem1/.../elemN</ul>
     * @param node the top of the tree to search.
     * @param path the path from the top of the tree to the desired element.
     * @return the first element matching the path, or null if no element
     * exists at the path location or if the starting node is not an element.
     */
    public static Element getElementViaPath(Node node, String path) {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();
        if (!(node instanceof Element))
            return null;
        int k = path.indexOf("/");
        String firstPathElement = path;
        if (k > 0)
            firstPathElement = path.substring(0, k);
        if (node.getNodeName().equals(firstPathElement)) {
            if (k < 0)
                return (Element) node;
            path = path.substring(k + 1);
            NodeList nodeList = ((Element) node).getChildNodes();
            Node n;
            for (int i = 0; i < nodeList.getLength(); i++) {
                n = nodeList.item(i);
                if ((n instanceof Element) && ((n = getElementViaPath(n, path)) != null))
                    return (Element) n;
            }
        }
        return null;
    }

    /**
     * Get the text content of an element identified by a path,
     * where the path elements can include an index. The first
     * path element must not have an index, and its name must
     * match the name of the starting node. If the starting
     * node is a Document, the root Element of the document is
     * used as the starting point. Path elements must be separated
     * by the slash character. If the path starts with a slash,
     * the slash is ignored. If the element or attribute identified
     * by the path is not present as a child of the starting node,
     * the empty string is returned. If a path element identifies
     * an attribute, any subsequent path elements are ignored.
     * A path is in the form: /e1/e2/e3/... or /e1/e2/@attr
     * Note the slash preceding the attribute's @-sign.
     * @param node the starting node for the path. The first path
     * element must match the name of this node.
     * @param path the path to the target node.
     * @return the full text value of the target node (including all
     * descendent text nodes), or the empty string if the target is
     * not a descendent of the starting node.
     */
    public static String getTextContent(Node node, String path) {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();
        if (!(node instanceof Element))
            return "";
        Element el = (Element) node;
        path = path.replaceAll("\\s", "");
        if (path.startsWith("/"))
            path = path.substring(1);
        String[] pathElements = path.split("/");
        if (!pathElements[0].equals(el.getTagName()))
            return "";
        for (int i = 1; i < pathElements.length; i++) {
            String pe = pathElements[i];
            if (pe.startsWith("@")) {
                //If this path element identifies an attribute, return it
                //and ignore any further path elements.
                return el.getAttribute(pe.substring(1));
            } else {
                //This path element identifies an Element. It may have an index.
                //Get the index, if present, and get the element name.
                int n = 0;
                int k = pe.indexOf("[");
                int kk = pe.lastIndexOf("]");
                if ((k != -1) && (k < kk)) {
                    try {
                        n = Integer.parseInt(pe.substring(k + 1, kk));
                    } catch (Exception ex) {
                        return "";
                    }
                    pe = pe.substring(0, k);
                } else if (k != kk)
                    return "";
                //We now have the element name and the index.
                //Find the identified Element. We have to count
                //matching elements to find the one identified
                //by the index.
                int nn = 0;
                Node child = el.getFirstChild();
                while (child != null) {
                    if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(pe)) {
                        if (n == nn)
                            break;
                        nn++;
                    }
                    child = child.getNextSibling();
                }
                //If the child is null, we didn't find the identified Element.
                if (child == null)
                    return "";
                //If we get here, we found it, now look for the next one.
                el = (Element) child;
            }
        }
        //Okay, we must be at the end of the path, and it must be an Element.
        //Return the text content of the element.
        return el.getTextContent();
    }
}

Related

  1. getNXInfo(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  2. getNXInfo1(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  3. getNXInfoDefault(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  4. getPath(Node n)
  5. getPath(Node node)
  6. getValueViaPath(Node node, String path)
  7. getVectorPathFromNode(Node node)
  8. getVectorPathFromNode(Node node)
  9. getVectorPathFromNode(Node node)