Example usage for org.w3c.dom Document importNode

List of usage examples for org.w3c.dom Document importNode

Introduction

In this page you can find the example usage for org.w3c.dom Document importNode.

Prototype

public Node importNode(Node importedNode, boolean deep) throws DOMException;

Source Link

Document

Imports a node from another document to this document, without altering or removing the source node from the original document; this method creates a new copy of the source node.

Usage

From source file:Main.java

public static Document documentFromNode(Element element) {
    try {//from www . j  av a 2s. c  o  m
        DocumentBuilder dBuilder = getDocumentBuilder();
        Document newDocument = dBuilder.newDocument();
        Node importedNode = newDocument.importNode(element, true);
        newDocument.appendChild(importedNode);
        return newDocument;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getNodeTextByXPath(Element eoEl, String xpath) {
    try {//from   w  ww  .  j  a v  a2 s  .c o  m
        Document doc = newDocument();
        Element importedEl = (Element) doc.importNode(eoEl.cloneNode(true), true);
        doc.appendChild(importedEl);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpathEn = factory.newXPath();
        return xpathEn.evaluate(xpath, doc);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}

From source file:Main.java

/**
 * Appends two documents/*from ww  w. ja  v  a 2 s. c  o m*/
 * 
 * @param curDoc      source document
 * @param appendDoc      document to append to source
 * @param eleNameToAdd   element tag name in source to which the appendDoc has to be appended as a child
 * @throws Exception
 */
public static void appendDocs(Document curDoc, Document appendDoc, String eleNameToAdd) throws Exception {

    Node importNode = curDoc.importNode(appendDoc.getDocumentElement(), true);
    curDoc.getElementsByTagName(eleNameToAdd).item(0).appendChild(importNode);

}

From source file:Main.java

public static String elementToString(final Node node) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final Document document = factory.newDocumentBuilder().newDocument();
    final Node importedNode = document.importNode(node, true);
    document.appendChild(importedNode);//w ww  .  j a  va2  s  .  c om
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(bos));
    return bos.toString("UTF-8");
}

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:Main.java

public static void importName(Document doc1, Document doc2) {
    Element root1 = doc1.getDocumentElement();
    Element personInDoc1 = (Element) root1.getFirstChild();

    Node importedPerson = doc2.importNode(personInDoc1, true);

    Element root2 = doc2.getDocumentElement();
    root2.appendChild(importedPerson);// w w w . j  ava 2s .c  o  m
}

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

From source file:Main.java

public static Document convertToDocument(Node node) throws ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);/*  w  w  w.  j  a  v  a 2  s . co  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document newDocument = builder.newDocument();
    newDocument.setXmlStandalone(true);
    Node importedNode = newDocument.importNode(node, true);
    newDocument.appendChild(importedNode);
    return newDocument;
}

From source file:Main.java

private static Document documentFromSoapBody(SOAPBodyElement element,
        HashMap<String, String> namespaceDeclarations) throws ParserConfigurationException {
    Document document = dbf.newDocumentBuilder().newDocument();
    Node node = document.importNode(element, true);
    document.appendChild(node);/*from w w  w  .java 2s.  com*/

    for (String prefix : namespaceDeclarations.keySet()) {
        String uri = namespaceDeclarations.get(prefix);
        if (node.lookupNamespaceURI(prefix) == null) {
            document.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix,
                    uri);
        }
    }
    return document;
}

From source file:Main.java

public static String transformXmlToString(Document doc, String encoding)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();

    Document document = builder.newDocument();

    Element e = doc.getDocumentElement();
    Node n = document.importNode(e, true);
    document.appendChild(n);/*www  .jav  a2s  .  c o  m*/

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);

    StringWriter sw = new StringWriter();

    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);

    return sw.toString();
}