Example usage for org.w3c.dom Document adoptNode

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

Introduction

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

Prototype

public Node adoptNode(Node source) throws DOMException;

Source Link

Document

Attempts to adopt a node from another document to this document.

Usage

From source file:Main.java

public static void main(final String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);// w  w  w  . ja  v  a 2  s.c o m
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new ByteArrayInputStream(( //
    "<?xml version=\"1.0\"?>" + //
            "<people>" + //
            "<person><name>First Person Name</name></person>" + //
            "<person><name>Second Person Name</name></person>" + //
            "</people>" //
    ).getBytes()));

    String fragment = "<name>Changed Name</name>";
    Document fragmentDoc = builder.parse(new ByteArrayInputStream(fragment.getBytes()));

    Node injectedNode = doc.adoptNode(fragmentDoc.getFirstChild());

    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile("//people/person[2]/name");
    Element nodeFound = (Element) expr.evaluate(doc, XPathConstants.NODE);

    Node parentNode = nodeFound.getParentNode();
    parentNode.removeChild(nodeFound);
    parentNode.appendChild(injectedNode);

    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(domSource, result);
    System.out.println(result.getWriter().toString());
}

From source file:Main.java

/**
 * @param target// w w  w. j  a  v  a 2 s.  c om
 * @param node
 * @return
 */
public static Node copyNode(Element target, Node node) {
    Node n = node.cloneNode(true);
    Document targetDoc = target.getOwnerDocument();
    targetDoc.adoptNode(n);
    target.appendChild(n);
    return node;
}

From source file:Main.java

public static void cloneAndAppend(Document document, Node node) {
    node = node.cloneNode(true);/*from  w  w w . ja v  a  2  s . c o  m*/
    document.adoptNode(node);
    document.appendChild(node);
}

From source file:Main.java

/**
 * Returns a new copy of the given node using the specified document as a
 * factory for new nodes.//from   ww  w . j  a va 2  s .co m
 * 
 * @param doc the document used as a factory for new nodes
 * @param node the node to copy
 * @return a new copy of the given node using the specified document as a
 *         factory for new nodes.
 */
@SuppressWarnings("unchecked")
public static <E extends Node> E newCopy(Document doc, E node) {
    E result = (E) node.cloneNode(true);
    return (E) doc.adoptNode(result);
}

From source file:Main.java

/**
 * @param target/*from w  w w.j  a  v a2s  . c  o  m*/
 * @param node
 * @return
 * @throws Exception
 */
public static Document copyDocument(Document document) throws Exception {
    Node n = document.getDocumentElement().cloneNode(true);
    Document result = newDocument();
    result.adoptNode(n);
    result.appendChild(n);
    return result;
}

From source file:Main.java

/**
 * Creates a document that has a deep copy of the element passed in as its document element
 * @param element/* w  w  w.  j  a  v  a 2s  .  c om*/
 * @return the new document
 */
public static Document createDocumentFrom(Element element) {
    Document document = createEmptyDocument();
    Node clonedNode = element.cloneNode(true);
    clonedNode = document.adoptNode(clonedNode);
    document.appendChild(clonedNode);
    return document;
}

From source file:Main.java

/**
 * Copies a document's content to another document (i.e. the root node is
 * adopted by the new document and inserted there). The new document should
 * be empty.//  ww  w.jav  a  2  s . c  o m
 *
 * @param oldDocument
 * @param newDocument
 *
 * @return the new containing document
 */
public static Document copyDocument(Document oldDocument, Document newDocument) {

    Node root = oldDocument.getDocumentElement();

    Node adoptedRoot = newDocument.adoptNode(root);

    newDocument.appendChild(adoptedRoot);

    return newDocument;
}

From source file:Main.java

private static Document pruneDocument(Document doc, int maxElement) throws ParserConfigurationException {
    if (maxElement == -1) {
        return doc;
    }/*w ww . j av  a2  s  .co  m*/

    Document newDoc = (Document) doc.cloneNode(false);
    Element newRoot = (Element) doc.getDocumentElement().cloneNode(false);
    newDoc.adoptNode(newRoot);
    newDoc.appendChild(newRoot);

    NodeList nl = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < maxElement && i < nl.getLength(); i++) {
        if (!(nl.item(i) instanceof Element)) {
            maxElement++;
            continue;
        }

        Node item = nl.item(i).cloneNode(true);
        newDoc.adoptNode(item);
        newDoc.getDocumentElement().appendChild(item);
    }

    if (debug)
        System.out.println("Creating document of " + newDoc.getDocumentElement().getChildNodes().getLength());
    return newDoc;
}

From source file:Main.java

/**
 * Tranform a node list into a document fragment
 *//* ww  w  . ja  va2s. c  o  m*/
public static Node toDocumentFragment(NodeList list) {
    if (list.getLength() == 1 && list.item(0) instanceof Document)
        return list.item(0);

    Document document = newDocument();
    DocumentFragment fragment = document.createDocumentFragment();
    for (int i = 0; i < list.getLength(); i++)
        fragment.appendChild(document.adoptNode(list.item(i).cloneNode(true)));
    return fragment;
}

From source file:Main.java

/**
 * Creates a XML document with the given root element and the given {@link NodeList} as content.
 * @param root The root element of the XML document.
 * @param content Content of the XML document.
 * @return The created XML document./*from  w ww .ja  v a2  s.c o m*/
 */
public static Document createDocument(String root, NodeList content) {
    DocumentBuilder docBuilder = createDocumentBuilder();
    Document document = docBuilder.newDocument();
    Element rootElement = document.createElement(root);
    document.appendChild(rootElement);

    for (int i = 0; i < content.getLength(); i++) {
        Node item = content.item(i);
        item = document.adoptNode(item.cloneNode(true));
        rootElement.appendChild(item);
    }
    return document;
}