List of utility methods to do XML Node Parent
Map | getParentNamespaces(Element el) This method traverses the DOM and grabs namespace declarations on parent elements with the intent of preserving them for children. HashMap<String, String> pref = new HashMap<String, String>(); Map<String, String> mine = getMyNamespaces(el); Node n = el.getParentNode(); while (n != null && n.getNodeType() != Node.DOCUMENT_NODE) { if (n instanceof Element) { Element l = (Element) n; NamedNodeMap nnm = l.getAttributes(); int len = nnm.getLength(); ... |
Node | getParentNode(Node node) get Parent Node if (node == null) { return null; } else if (node instanceof Attr) { return ((Attr) node).getOwnerElement(); } else { return node.getParentNode(); |
Node | getParentNodeNoEx(Node node, String name) get Parent Node No Ex for (String str : name.split("\\.")) if (node == null) return null; else node = getNodeByName(node, str); return node; |
Node | getParentOfNode(Node node) Get the XPath-model parent of a node. Node parent = node.getParentNode(); if (parent == null && (Node.ATTRIBUTE_NODE == node.getNodeType())) parent = ((Attr) node).getOwnerElement(); return parent; |
Node | getParentOfNode(Node node) Obtain the XPath-model parent of a DOM node -- ownerElement for Attrs, parent for other nodes. Node parent; short nodeType = node.getNodeType(); if (Node.ATTRIBUTE_NODE == nodeType) { Document doc = node.getOwnerDocument(); DOMImplementation impl = doc.getImplementation(); if (impl != null && impl.hasFeature("Core", "2.0")) { parent = ((Attr) node).getOwnerElement(); return parent; ... |
List | getParents(Node node) get Parents List list = new ArrayList(); node = node.getParentNode(); while (node != null) { list.add(node); node = node.getParentNode(); return list; |
boolean | isAncestor(Node ancestor, Node node) Verify if ancestor is an ancestor of node
Node p = node; while ((p = p.getParentNode()) != null) { if (ancestor == p) return true; return false; |
boolean | isAncestorOf(Node ancestor, Node node) is Ancestor Of while (node != null) { if (node == ancestor) { return true; node = node.getParentNode(); return false; |
boolean | isAncestorOf(Node candidateAncestor, Node candidateSibling, boolean strict) Checks whether a node is ancestor or same of another node. if (candidateAncestor == null) throw new NullPointerException("candidate ancestor cannot be null null."); if (candidateSibling == null) throw new NullPointerException("candidate sibling cannot be null null."); if (strict && candidateAncestor.equals(candidateSibling)) return false; Node parent = candidateSibling; while (parent != null) { ... |
boolean | isAncestorOf(Node node, Node descendant) Checks whether a node is ancestor of another node. if (node == null || descendant == null) { return false; for (Node currentNode = descendant.getParentNode(); currentNode != null; currentNode = currentNode .getParentNode()) { if (currentNode == node) { return true; return false; |