Java Utililty Methods XML Node Path

List of utility methods to do XML Node Path

Description

The list of methods to do XML Node Path are organized into topic(s).

Method

ElementaddElementPath(Node element, String path)
add Element Path
return addElementPath(element, path.split("/"));
NodecreateAndAppendNode(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;
...
ListextractNodes(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)));
...
NodeextractPath(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;
ListextractPaths(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;
StringgetCompletePathForANode(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;
...
StringgetContent(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);
ElementgetDescendant(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;
ElementgetElementViaPath(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);
...
StringgetFullPath(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);
...