Java XML Node Path getNodePath(Node node)

Here you can find the source of getNodePath(Node node)

Description

get Node Path

License

Open Source License

Declaration

public static final String getNodePath(Node node) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Stack;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    public static final String getNodePath(Node node) {
        StringBuilder buffer = new StringBuilder("/");
        Stack<String> stack = new Stack<String>();
        Node n = node;//from   ww  w. j a va2 s  .c o  m

        while (n != null) {
            String nname = n.getNodeName();
            NamedNodeMap attrs = n.getAttributes();
            if (attrs != null && attrs.getLength() != 0) {
                Node attr = attrs.getNamedItem("xconf-uid");
                if (attr != null) {
                    nname += "[@xconf-uid=\"" + attr.getNodeValue() + "\"]";
                }
            }
            stack.push(nname);
            n = n.getParentNode();
        }

        if (stack.size() >= 3) {
            stack.pop();
            stack.pop();
        } else {
            return null;
        }

        while (!stack.empty()) {
            buffer.append("/");
            buffer.append(stack.pop());
        }

        return buffer.toString();
    }
}

Related

  1. getNode(Node node, String... nodePath)
  2. getNode(Node root, String nodePath)
  3. getNodeCompletePath(Node node)
  4. getNodePath(Node node)
  5. getNodePath(Node node)
  6. getNodePath(Node node, String postfix)
  7. getNodes(Node node, String path)
  8. getNodesPathName(Node node)
  9. getNXInfo(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)