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 Node up(Node node, String element) {
    while (node != null && node.getNodeName() != null && !node.getNodeName().equalsIgnoreCase(element)) {
        node = node.getParentNode();
    }/*from   ww  w .  j  a v a  2s .  c  o  m*/
    return node;
}

From source file:Main.java

private final static Node getParent(final Node n) {
    return n instanceof Attr ? ((Attr) n).getOwnerElement() : n.getParentNode();
}

From source file:Main.java

public static String getXPathForElement(Node e, NamespaceContext ctx) {
    StringBuffer sb = new StringBuffer();
    List<Node> path = new ArrayList<Node>();

    Node currentNode = e;
    while (currentNode.getParentNode() != currentNode.getOwnerDocument()) {
        path.add(0, currentNode);/*  ww w .  j  av a2  s .c  o  m*/
        if (currentNode instanceof Attr) {
            Attr a = (Attr) currentNode;
            currentNode = a.getOwnerElement();
        } else {
            currentNode = currentNode.getParentNode();
        }
    }
    path.add(0, currentNode); // We need the root element

    for (Node n : path) {
        sb.append("/");

        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            sb.append("@");
        }

        String namespaceURI = n.getNamespaceURI();
        if (namespaceURI != null && !namespaceURI.equals("")) {
            sb.append(ctx.getPrefix(namespaceURI)).append(":");
        }
        sb.append(n.getLocalName());

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            appendElementQualifier(sb, (Element) n);
        }
    }

    return sb.toString();
}

From source file:Main.java

public static void removeElementXML(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  . ja  v  a2  s .  com
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeElementXML(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static String nodeTreeToString(Node node) {
    String strNode = "";
    Node nodeIt = node;
    while (nodeIt != null) {
        strNode = nodeToString(nodeIt) + strNode;

        nodeIt = nodeIt.getParentNode();
    }//  ww w.ja v  a  2 s  .  co  m
    return strNode;
}

From source file:Main.java

private static Vector getVectorPathFromNode(Node node) {
    Vector path = new Vector();

    while (node != null) {
        path.insertElementAt(node, 0);//from  www .  ja  va 2  s.c  o  m
        node = node.getParentNode();
    }

    return path;
}

From source file:Main.java

/**
 * Returns absolute xpath for specified element, ignores namespaces
 * /*from  w w  w.j  av a  2s.c o m*/
 * @param element
 *           the element to create for
 * @return the elements path in its containing document
 */

public static String getElementPath(Element element) {
    Node elm = element;

    String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]";
    while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) {
        elm = elm.getParentNode();
        result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result;
    }

    return "/" + result;
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java

public static String formatLexML(String xml) {
    String retorno = null;//from   w  w w.j  a va  2s.c o m
    try {
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        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);
        }

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);

        transformer.transform(new DOMSource(document), streamResult);

        retorno = stringWriter.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return retorno;

}

From source file:Main.java

public static String getPath(Node node) {
    String path = "";
    Node current = node;
    while ((current != null) && !(current instanceof Document)) {
        path = current.getNodeName() + "/" + path;
        current = current.getParentNode();
    }//from  w ww.  j av  a2  s .co  m
    if (path != null) {
        path = path.substring(0, path.lastIndexOf("/"));
    }
    return path;
}

From source file:Main.java

public static String toPrettyString(String xml) {
    int indent = 4;
    try {//from w w  w.  java2 s .c  o 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);
        }

        // Setup pretty print options
        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, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        // Return pretty print xml string
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        System.out.println(xml);
        throw new RuntimeException(
                "Problems prettyfing xml content [" + Splitter.fixedLength(100).split(xml) + "]", e);
    }
}