Example usage for org.w3c.dom Node getParentNode

List of usage examples for org.w3c.dom Node getParentNode

Introduction

In this page you can find the example usage for org.w3c.dom Node getParentNode.

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }//  w  w w . j  a v  a  2 s.  c  o m
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

public static String getNodePath(Node node) {
    StringBuffer sb = new StringBuffer(32);
    while (node != null) {
        sb.append(node.getNodeName());//  w  w  w .jav a2 s.c  o m
        sb.append("/");
        node = node.getParentNode();
    }
    return sb.toString();
}

From source file:Main.java

public static void moveUp(Node currentN) {
    Node prevSibling = findPreviousElement(currentN, false);
    if (prevSibling != null) {
        Node parent = currentN.getParentNode();
        parent.removeChild(currentN);//from ww w  .  j  ava  2  s.  c  o m
        parent.insertBefore(currentN, prevSibling);
    }

}

From source file:Main.java

/**
 * Method to get the depth of a XML Element.
 * @param element the XML Element.//from ww w  .  j a  v  a  2s.c o  m
 * @return the int depth of the XML Element.
 */
public static int getDepth(Element element) {
    Node parent = element.getParentNode();
    int depth = 0;
    while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
        depth++;
        parent = parent.getParentNode();
    }
    return depth;
}

From source file:Main.java

public static void removeAll(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {//from   w w  w  .  j a  va  2s  .c o  m
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

/**
 * Remove named nodes of the specified nodeType from the specified node.
 * // w  w  w.j a va 2 s  .  c om
 * @param node the node to be cleaned.
 * @param nodeType the type of nodes to be removed.
 * @param name the name of nodes to be removed.
 */
public static void removeAll(final Node node, final short nodeType, final String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {
        // Visit the children
        final NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static void moveUp(final Node currentN) {
    Node prevSibling = findPreviousElement(currentN, false);
    if (prevSibling != null) {
        Node parent = currentN.getParentNode();
        parent.removeChild(currentN);/*  ww  w .j  a v a  2s  .c  o m*/
        parent.insertBefore(currentN, prevSibling);
    }

}

From source file:Utils.java

/**
 * Starting from a node, find the namespace declaration for a prefix. for a matching namespace
 * declaration.//from w ww .  j  av  a  2  s . c  om
 * 
 * @param node search up from here to search for namespace definitions
 * @param searchPrefix the prefix we are searching for
 * @return the namespace if found.
 */
public static String getNamespace(Node node, String searchPrefix) {

    Element el;
    while (!(node instanceof Element)) {
        node = node.getParentNode();
    }
    el = (Element) node;

    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node currentAttribute = atts.item(i);
        String currentLocalName = currentAttribute.getLocalName();
        String currentPrefix = currentAttribute.getPrefix();
        if (searchPrefix.equals(currentLocalName) && XMLNAMESPACE.equals(currentPrefix)) {
            return currentAttribute.getNodeValue();
        } else if (isEmpty(searchPrefix) && XMLNAMESPACE.equals(currentLocalName) && isEmpty(currentPrefix)) {
            return currentAttribute.getNodeValue();
        }
    }

    Node parent = el.getParentNode();
    if (parent instanceof Element) {
        return getNamespace((Element) parent, searchPrefix);
    }

    return null;
}

From source file:Main.java

public static Node getEnclosingNodeWithName(Node node, String name) {
    while (node != null) {
        if (node.getLocalName() != null && node.getLocalName().equals(name)) {
            return node;
        }//ww w .  jav a 2 s. co  m
        node = node.getParentNode();
    }
    return null;
}

From source file:Main.java

public static synchronized boolean deleteNode(String charCode, Node root) {
    try {/*from w ww  .  j  a v  a 2s .  co m*/
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node node = (Node) xpath.evaluate(String.format(XPATH_EVAL_ID, charCode), root, XPathConstants.NODE);
        if (node != null) { // remove existing node
            Node parent = node.getParentNode();
            parent.removeChild(node);
            return true;
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return false;
}