Here you can find the source of getValueViaPath(Node node, String path)
Parameter | Description |
---|---|
node | the top of the tree to search. |
path | the path from the top of the tree to the desired node. |
public static String getValueViaPath(Node node, String path)
//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 a 2 s.co 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. * A path to an element has the form: elem1/.../elemN * A path to an attribute has the form: elem1/.../elemN@attr * 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 getElementValue(target); } //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. * A path to an element has the form: elem1/.../elemN * @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 value of an element. If the node is a Document, use the * document element as the element node. * 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 node. * @return the value of the element, or an empty string if the * node is not an element. */ public static String getElementValue(Node node) { if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return ""; NodeList nodeList = node.getChildNodes(); String value = ""; for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n.getNodeType() == Node.TEXT_NODE) value += n.getNodeValue(); } return value; } }