Java Utililty Methods XML Node Parent

List of utility methods to do XML Node Parent

Description

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

Method

MapgetParentNamespaces(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();
...
NodegetParentNode(Node node)
get Parent Node
if (node == null) {
    return null;
} else if (node instanceof Attr) {
    return ((Attr) node).getOwnerElement();
} else {
    return node.getParentNode();
NodegetParentNodeNoEx(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;
NodegetParentOfNode(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;
NodegetParentOfNode(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;
...
ListgetParents(Node node)
get Parents
List list = new ArrayList();
node = node.getParentNode();
while (node != null) {
    list.add(node);
    node = node.getParentNode();
return list;
booleanisAncestor(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;
booleanisAncestorOf(Node ancestor, Node node)
is Ancestor Of
while (node != null) {
    if (node == ancestor) {
        return true;
    node = node.getParentNode();
return false;
booleanisAncestorOf(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) {
...
booleanisAncestorOf(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;