Here you can find the source of extractPath(Node node, String[] path)
Parameter | Description |
---|---|
node | Node to apply path to |
path | Path to apply |
static public Node extractPath(Node node, String[] path)
//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; } }