List of utility methods to do XML Node Path
Element | addElementPath(Node element, String path) add Element Path return addElementPath(element, path.split("/")); |
Node | createAndAppendNode(Node root, String path) create And Append Node if (path == null) return root; Element node = null; if (path.contains("/")) String[] steps = path.split("/"); Node upper = root; Node lower = null; ... |
List | extractNodes(Node node, String path) Returns all nodes at the bottom of path from node. if (node == null) return new ArrayList<Node>(); List<Node> result = new ArrayList<Node>(); NodeList list = node.getChildNodes(); if (path.equals("#text")) result.add(node.getFirstChild()); else if (path.charAt(0) == '@') result.add(node.getAttributes().getNamedItem(path.substring(1))); ... |
Node | extractPath(Node node, String[] path) Returns first node at the bottom of path from node. for (int i = 0; i < path.length; i++) node = extractNode(node, path[i]); return node; |
List | extractPaths(Node node, String[] path) Returns all nodes at the bottom of path from node. List<Node> result = new ArrayList<Node>(); result.add(node); for (int i = 0; i < path.length; i++) { List<Node> children = new ArrayList<Node>(); for (int j = 0; j < result.size(); j++) children.addAll(extractNodes((Node) result.get(j), path[i])); result = children; return result; |
String | getCompletePathForANode(Node objNode) get Complete Path For A Node String strCompletePathForNode = objNode.getNodeName(); Node tempNode = objNode; while (tempNode.getParentNode() != null) { tempNode = tempNode.getParentNode(); if (tempNode.getNodeName().equals("#document")) strCompletePathForNode = "/" + strCompletePathForNode; else strCompletePathForNode = tempNode.getNodeName() + "/" + strCompletePathForNode; ... |
String | getContent(Node n, String path) Get the text at specified path starting from the specified node if (n == null) return null; return getContent(n.getChildNodes(), path); |
Element | getDescendant(Node node, String path) Gets a descendant element of a node. StringTokenizer tok = new StringTokenizer(path, "/"); while (tok.hasMoreTokens()) { node = getChild(node, tok.nextToken()); if (node == null) return null; return (Element) node; |
Element | getElementViaPath(Node node, String path) Get an element specified by a starting node and a path string. 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); ... |
String | getFullPath(Node node) get Full Path StringBuilder buffer = new StringBuilder(); while (node != null) { buffer.insert(0, node.getNodeName()); char separator = '/'; if (node instanceof Attr) { separator = '@'; buffer.insert(0, separator); ... |