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 List<Element> getChildElements(Element parent, String tagName) {
    if (null == parent)
        return null;
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getParentNode() == parent)
            elements.add((Element) node);
    }/*w ww . j  a  v  a  2  s .c o  m*/

    return elements;
}

From source file:Main.java

/**
 * Generates a path expression that refers to
 * the given element./*from w ww  .  ja v  a 2s.  co m*/
 * @param elem the element for which a path is
 *        generated.
 * @return a path from the root to the given element.
 */
public static String path(Element elem) {
    String path = "/" + name(elem);
    Node currentNode = elem;
    while ((currentNode = currentNode.getParentNode()) != null) {
        if (currentNode instanceof Element) {
            path = "/" + name((Element) currentNode) + path;
        } else {
            break;
        }
    }
    return path;
}

From source file:Main.java

/**
 * Builds the node path expression for a node in the DOM tree.
 * @param node in a DOM tree./*ww  w  .  j  a v a2  s  .c o  m*/
 * @param buffer string buffer.
 */
private static void buildNodeName(Node node, StringBuffer buffer) {
    if (node.getParentNode() == null) {
        return;
    }

    buildNodeName(node.getParentNode(), buffer);

    if (node.getParentNode() != null && node.getParentNode().getParentNode() != null) {
        buffer.append(".");
    }

    buffer.append(node.getLocalName());
}

From source file:Main.java

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

From source file:Main.java

public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    ArrayList 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);
        }/*from w  ww . j a  va 2  s .  c o m*/
    }

    return elements;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    NodeList nodes = parent.getChildNodes();
    ArrayList 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);
        }/*  www.j ava  2 s .c o  m*/
    }

    return elements;
}

From source file:Main.java

/**
 * Check if a node in the parent hierarchy has the name given in parameter
 * /*from  w w  w.  ja va  2  s .  c om*/
 * @param node
 * @param string
 * @return true if the node exists
 */
public static boolean containsInHierarchy(Node node, String string) {
    Node parent = node;
    if (node != null) {
        do {
            parent = parent.getParentNode();
        } while (parent != null && !parent.getNodeName().equals(string));
    }
    return parent != null;
}

From source file:Main.java

public static void replaceXPathNode(Node replaceThis, Node replaceWith) throws XPathExpressionException {
    Node parentNode = replaceThis.getParentNode();
    parentNode.insertBefore(replaceWith, replaceThis);
    parentNode.removeChild(replaceThis);
}

From source file:Main.java

public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }//  www.j a  v  a  2s .com
    }
    return elements;
}

From source file:Main.java

public static String prettyXML(String xml, int indent) {
    try {//from w w w.  java  2s. co  m
        // Turn xml string into a document
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        // Remove whitespaces outside tags
        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);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}