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

/**
 * Gets the ancestor element node to the given node.
 * //from   w ww  .  j av  a  2  s.  co  m
 * @param currentNode the node to retrive the ancestor for
 * 
 * @return the ancestral element node of the current node, or null
 */
public static Element getElementAncestor(Node currentNode) {
    Node parent = currentNode.getParentNode();
    if (parent != null) {
        short type = parent.getNodeType();
        if (type == Node.ELEMENT_NODE) {
            return (Element) parent;
        }
        return getElementAncestor(parent);
    }
    return null;
}

From source file:Main.java

/**
 * @param elem/*from   w  ww.ja va2 s .  c om*/
 * @param childTag
 * @return
 */
public static Element getFirstChild(Element elem, String childTag) {
    if (elem.hasChildNodes()) {
        NodeList list = elem.getElementsByTagName(childTag);
        int count = list.getLength();

        for (int i = 0; i < count; i++) {
            Node node = list.item(i);
            if (node.getParentNode() != elem)
                continue;

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Node replaceNode(Document ImpD, Node oldE, Node newE) {
    Node Parent = oldE.getParentNode();
    Node ImpNewNode = ImpD.importNode(newE, true);
    Parent.replaceChild(ImpNewNode, oldE);

    return Parent;
}

From source file:Utils.java

/**
 * Search up the tree for a given node/*from w  w  w.j  a  va  2s  .co  m*/
 * @param currentNode Starting point for our search
 * @param tagName Node name to look up
 * @return matching Node (null if none)
 */
public static Node getPreviousNodeByName(Node currentNode, String tagName) {
    Node node = currentNode.getParentNode();

    while ((node != null) && (!node.getNodeName().equals(tagName))) {
        node = node.getParentNode();
    }
    return node;
}

From source file:Main.java

private static void stripNamedElements(Document document, String name) {
    Element root = document.getDocumentElement();
    if (root != null) {
        NodeList nodes = root.getElementsByTagName(name);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            node.getParentNode().removeChild(node);
        }/*from   ww  w  . j  a v  a  2  s.  com*/
    }
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    NodeList nodeList = element.getElementsByTagName(tagName);
    List<Element> elements = new java.util.ArrayList<Element>(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child.getParentNode() == element) {
            elements.add((Element) child);
        }/*from w  w  w .  j a  va2s . com*/
    }
    return elements;
}

From source file:Main.java

/**
 * Removes the Leaf Node//  w  ww  . ja v  a2 s. co  m
 */

public static void removeLeafNode(Node node)

{

    if (!node.hasChildNodes())

        node.getParentNode().removeChild(node);

}

From source file:Main.java

public static long getStartTime(Node p) {
    long time = 0;
    Node current = p;
    while ((current = current.getParentNode()) != null) {
        if (current.getAttributes() != null && current.getAttributes().getNamedItem("begin") != null) {
            time += toTime(current.getAttributes().getNamedItem("begin").getNodeValue());
        }// w w  w.  j a va  2s.com
    }

    if (p.getAttributes() != null && p.getAttributes().getNamedItem("begin") != null) {
        return time + toTime(p.getAttributes().getNamedItem("begin").getNodeValue());
    }
    return time;
}

From source file:Main.java

public static long getEndTime(Node p) {
    long time = 0;
    Node current = p;
    while ((current = current.getParentNode()) != null) {
        if (current.getAttributes() != null && current.getAttributes().getNamedItem("begin") != null) {
            time += toTime(current.getAttributes().getNamedItem("begin").getNodeValue());
        }/*from w w w  .j  a  v  a  2s  .com*/
    }

    if (p.getAttributes() != null && p.getAttributes().getNamedItem("end") != null) {
        return time + toTime(p.getAttributes().getNamedItem("end").getNodeValue());
    }
    return time;
}

From source file:Main.java

public static Node replaceNode(final Document impD, final Node oldE, final Node newE) {
    Node parent = oldE.getParentNode();
    Node impNewNode = impD.importNode(newE, true);
    parent.replaceChild(impNewNode, oldE);

    return parent;
}