Example usage for org.w3c.dom Document createDocumentFragment

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

Introduction

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

Prototype

public DocumentFragment createDocumentFragment();

Source Link

Document

Creates an empty DocumentFragment object.

Usage

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    DocumentFragment frag = xmlDoc.createDocumentFragment();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  www .j  ava2s  . c o  m*/

    Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml"));

    String fragment = "<fragment>aaa</fragment>";

    factory = DocumentBuilderFactory.newInstance();
    Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment)));

    Node node = doc.importNode(d.getDocumentElement(), true);

    DocumentFragment docfrag = doc.createDocumentFragment();

    while (node.hasChildNodes()) {
        docfrag.appendChild(node.removeChild(node.getFirstChild()));
    }

    Element element = doc.getDocumentElement();
    element.appendChild(docfrag);
}

From source file:Main.java

/**
 * Tranform a node list into a document fragment
 *///from   ww w  . ja v a 2s .  c  om
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

public static void addFragment(Document doc) {
    Element person;/*from   ww w  .  j a  v a2  s  . c o m*/
    Element root = doc.getDocumentElement();
    DocumentFragment fragment = doc.createDocumentFragment();
    person = makePersonNode(doc, "Name 1", "555-4444");
    fragment.appendChild(person);
    person = makePersonNode(doc, "Name 2", "555-9999");
    fragment.appendChild(person);
    root.appendChild(fragment);
}

From source file:Main.java

/**
 * Will create a documentFragment of the replacingDocument, will import the
 * replacingDocument as a node of the replacedDocument, and then will
 * replace the replaceNode with the documentFragment of replacingDocument.
 * //w w w.j a  v  a  2  s .com
 * @param replacedDocument
 *            The document which will have a node replace
 * @param replacingDocument
 *            The document that will replace a node
 * @param replacedNode
 *            The node in replacedDocument that will be replaced
 * @return The new version of replacedDocument will replacedNode replaced
 */
public static Node replaceNode(Document replacedDocument, Document replacingDocument, Node replacedNode) {

    // Create a documentFragment of the replacingDocument
    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    // Import docFrag under the ownership of replacedDocument
    Node replacingNode = ((replacedDocument).importNode(docFrag, true));

    // In order to replace the node need to retrieve replacedNode's parent
    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
}

From source file:Main.java

/**
 * Will create a documentFragment of the replacingDocument, will import the
 * replacingDocument as a node of the replacedDocument, and then will
 * replace the replaceNode with the documentFragment of replacingDocument.
 * //www. j a  va  2s  .  c o m
 * @param replacedDocument
 *            The document which will have a node replace
 * @param replacingDocument
 *            The document that will replace a node
 * @param replacedNode
 *            The node in replacedDocument that will be replaced
 * @return The new version of replacedDocument will replacedNode replaced
 */
public static Node replaceNode(final Document replacedDocument, final Document replacingDocument,
        final Node replacedNode) {

    // Create a documentFragment of the replacingDocument
    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    // Import docFrag under the ownership of replacedDocument
    Node replacingNode = ((replacedDocument).importNode(docFrag, true));

    // In order to replace the node need to retrieve replacedNode's parent
    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
}

From source file:DOMEdit.java

public static void addFragment(Document doc) {
        Element person;/*from   www.java2  s . com*/
        Element root = doc.getDocumentElement();
        DocumentFragment fragment = doc.createDocumentFragment();
        person = makePersonNode(doc, "Name 1", "555-4444");
        fragment.appendChild(person);
        person = makePersonNode(doc, "Name 2", "555-9999");
        fragment.appendChild(person);
        root.appendChild(fragment);
    }

From source file:ee.ria.xroad.proxy.util.MetaserviceTestUtil.java

/** Merge xroad-specific {@link SoapHeader} to the generic {@link SOAPHeader}
 * @param header//  w w w  .  j  a va 2s .  co m
 * @param xrHeader
 * @throws JAXBException
 * @throws ParserConfigurationException
 * @throws SOAPException
 */
public static void mergeHeaders(SOAPHeader header, SoapHeader xrHeader)
        throws JAXBException, ParserConfigurationException, SOAPException {

    Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
    final DocumentFragment documentFragment = document.createDocumentFragment();
    // marshalling on the header would add the xroad header as a child of the header
    // (i.e. two nested header elements)
    marshaller.marshal(xrHeader, documentFragment);

    Document headerDocument = header.getOwnerDocument();
    Node xrHeaderElement = documentFragment.getFirstChild();

    assertTrue("test setup failed: assumed had header element but did not",
            xrHeaderElement.getNodeType() == Node.ELEMENT_NODE
                    && xrHeaderElement.getLocalName().equals("Header"));

    final NamedNodeMap attributes = xrHeaderElement.getAttributes();

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            final Attr attribute = (Attr) attributes.item(i);
            header.setAttributeNodeNS((Attr) headerDocument.importNode(attribute, false));
        }
    }

    final NodeList childNodes = xrHeaderElement.getChildNodes();

    if (childNodes != null) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            final Node node = childNodes.item(i);
            header.appendChild(headerDocument.importNode(node, true));
        }
    }

}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *//*from ww  w . j  a v a2 s . c  o m*/
public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if (target == null || node.getOwnerDocument() == target)
        // same Document
        return node.cloneNode(deep);
    else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null)
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep)
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
                newNode.appendChild(cloneNode(child, target, true));

        return newNode;
    }
}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *///  w ww. j av a 2s . c o m
public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if ((target == null) || (node.getOwnerDocument() == target)) {
        // same Document
        return node.cloneNode(deep);
    } else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null) {
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }
            }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep) {
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                newNode.appendChild(cloneNode(child, target, true));
            }
        }

        return newNode;
    }
}