Java XML Document Create createDocumentFromElement(final Element element)

Here you can find the source of createDocumentFromElement(final Element element)

Description

create Document From Element

License

Open Source License

Declaration

public static Document createDocumentFromElement(final Element element)
            throws ParserConfigurationException, SAXException, IOException 

Method Source Code

//package com.java2s;
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class Main {
    public static Document createDocumentFromElement(final Element element)
            throws ParserConfigurationException, SAXException, IOException {
        Document ret = null;/*from  www .  ja v  a2 s . co m*/

        if (null == element) {
            throw new NullPointerException("input DOM element is null");
        }

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        ret = factory.newDocumentBuilder().newDocument();
        Node domNode = ret.importNode(element, true);
        ret.appendChild(domNode);

        return ret;
    }

    public static Document createDocumentFromElement(final Node node)
            throws ParserConfigurationException, SAXException, IOException {
        Document ret = null;

        if (null == node) {
            throw new NullPointerException("input DOM node is null");
        }

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        ret = factory.newDocumentBuilder().newDocument();
        Node domNode = ret.importNode(node, true);
        ret.appendChild(domNode);

        return ret;
    }
}

Related

  1. createDocument(String xml)
  2. createDocumentBuilder()
  3. createDocumentBuilder(boolean namespaces, boolean validating)
  4. createDocumentBuilderFactory()
  5. createDocumentBuilderFactory(ClassLoader classLoader)
  6. createDocumentFromFile(File file)
  7. createDocumentFromResult( final StreamResult result)
  8. createDocumentFromString(String str)
  9. createDocumentFromString(String xmlString)