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 getpathToRoot(final Node nodeIn) {
    String path = "";
    Node parent = null;/*  w w w .  ja  v a 2s  .  com*/
    int y = 0;
    if (nodeIn != null) {
        Node locNode = nodeIn;
        while (locNode.getParentNode() != null) {

            parent = locNode.getParentNode();
            if (y > 0) {
                path = ":" + path;
            }
            y++;
            int i = getCurrentPosition(locNode);
            i = i - 1;
            // Check to see it has children as otherwise the htmltree has a
            // problem with expanding leaves.
            if (nodeIn.hasChildNodes()) {
                path = i + path;
            }
            locNode = parent;

        }
    }
    if (path == null || path.length() == 0) {
        path = "0";

    }

    return path;
}

From source file:Main.java

public static Element getFirstChildElement(Element element) {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            return (Element) node;
        }//w  w w.j  av  a2s.co m
    }
    return null;
}

From source file:Main.java

public static List<Element> elements(Element element) {
    List<Element> elements = new ArrayList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            elements.add((Element) node);
        }/*from w  w w .ja  v  a  2s .  co m*/
    }
    return elements;
}

From source file:Main.java

public static void writeTo(Document document, File output) {
    try {//from   w ww .  j  a  v  a  2 s  .com
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);

        FileOutputStream outputstream = new FileOutputStream(output);
        StreamResult result = new StreamResult(outputstream);

        // Manually add xml declaration, to force a newline after it.
        String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        outputstream.write(xmlDeclaration.getBytes());

        // Remove whitespaces outside tags.
        // Essential to make sure the nodes are properly indented.
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }

        // Pretty-print options.
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(source, result);

        outputstream.close();
    } catch (TransformerException | IOException | XPathExpressionException e) {
        System.out.println("Failed to write document file" + output.getPath() + ": " + e.toString());
    }
}

From source file:Main.java

public static Node insertTextNodeAfter(String text, Node sibling) {
    Text newNode = sibling.getOwnerDocument().createTextNode(text);
    return sibling.getParentNode().insertBefore(newNode, sibling.getNextSibling());
}

From source file:Main.java

/**
 * Gets the immediately child elements list from the parent element.
 *
 * @param parent/* ww w. j  a v  a2 s  .co m*/
 *          the parent element in the element tree
 * @param tagName
 *          the specified tag name
 * @return the NOT NULL immediately child elements list
 */
public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }
    }

    return elements;
}

From source file:Main.java

public static Node insertTextNodeBefore(String text, Node sibling) {
    Text newNode = sibling.getOwnerDocument().createTextNode(text);
    return sibling.getParentNode().insertBefore(newNode, sibling);
}

From source file:Main.java

/**
 * Get the sibling in next context//from w w  w  .  j  a v a 2 s.  c  o  m
 * 
 * @param current
 * @return the sibling
 */
public static Node getSibling(Node current) {
    Node sibling = current.getNextSibling();
    Node tmpParent = current;
    while (tmpParent != null && sibling == null) {
        tmpParent = tmpParent.getParentNode();
        if (tmpParent != null) {
            sibling = tmpParent.getNextSibling();
        }
    }
    return sibling;
}

From source file:Main.java

public static String getNodeDesc(Node node) {

    String tree = "";

    if (node == null)
        return tree;

    if (node.getParentNode() != null) {
        tree += getNodeDesc(node.getParentNode()) + ".";
    }/*from   ww  w .  ja  v  a 2s . c o m*/

    return tree + node.getNodeName();
}

From source file:Main.java

public static Node insertNewAfter(final Document impD, final Node targetE, final Node newE) {
    Node impNewNode = impD.importNode(newE, true);
    Node parent = targetE.getParentNode();
    if (targetE.getNextSibling() == null) {
        parent.appendChild(impNewNode);//from  w  w  w  .ja  v  a 2 s. c o m
    } else {
        parent.insertBefore(impNewNode, targetE.getNextSibling());
    }
    return impNewNode;
}