Example usage for javax.xml.parsers DocumentBuilder newDocument

List of usage examples for javax.xml.parsers DocumentBuilder newDocument

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder newDocument.

Prototype


public abstract Document newDocument();

Source Link

Document

Obtain a new instance of a DOM Document object to build a DOM tree with.

Usage

From source file:Main.java

public static Document creatXMLFile() throws ParserConfigurationException {
    Document doc = null;/*from w  w  w .  j  a va 2 s. com*/

    // Create instance of DocumentBuilderFactory
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Get the DocumentBuilder
    final DocumentBuilder parser = factory.newDocumentBuilder();
    // Create blank DOM Document

    doc = parser.newDocument();

    return doc;
}

From source file:Main.java

public static synchronized Element createRootElement() {
    Element rootElement = null;/* www.  j av  a2  s. c om*/
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.newDocument();
        rootElement = doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootElement;
}

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.//w  ww.j a va 2s.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;
}

From source file:Main.java

public static Document convertToDocument(Node node) throws ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);//  w w w  . ja 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

/**
 *  Create new XML document./*  ww w .  j  a  v a 2s.  c  om*/
 *
 * @param  rootElementName  name of the root element to add, or <code>null</code> if the
 *      document should not have any root just yet
 * @throws ParserConfigurationException 
 */
public static Document createEmptyDocument(String rootElementName, String namespace)
        throws ParserConfigurationException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    Document result = builder.newDocument();

    if (rootElementName != null) {
        Element rootElement;
        if (namespace != null && !namespace.isEmpty()) {
            rootElement = result.createElementNS(rootElementName, namespace);
        } else {
            rootElement = result.createElement(rootElementName);
        }
        result.appendChild(rootElement);
    }
    return result;
}

From source file:Main.java

public static void createEmptyXML222(String xmlFile) {
    try {//from w w w  .  j ava  2s  .  c  o m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static Document blankDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    localDocumentBuilderFactory.setIgnoringComments(false);
    localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
    localDocumentBuilderFactory.setValidating(false);
    localDocumentBuilderFactory.setCoalescing(false);
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}

From source file:Main.java

/**
 * Create an empty XML document for building up a DOM tree.
 * @return the document/*from  ww w  .  j a  v  a  2  s. c o m*/
 */
public static Document createEmptyDocument() {
    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.get().newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        return document;
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document makeEmptyXmlDocument(String rootElementName) {
    Document document = null;/*from  w  w  w .  j a v a2  s. c  o  m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(true);
    // factory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();

        document = builder.newDocument();
    } catch (Exception e) {
        //Debug.logError(e, module);
    }

    if (document == null)
        return null;

    if (rootElementName != null) {
        Element rootElement = document.createElement(rootElementName);
        document.appendChild(rootElement);
    }

    return document;
}

From source file:Main.java

/**
 * This method creates an empty <code>Document</code> object that can be used to store
 * data that will later be written out as an XML file.  It is adapted from 
 * code posted at //from w w w.j  av  a2s  .com
 * <a href="http://www.exampledepot.com/egs/javax.xml.parsers/CreateDom.html"
 *    target="_top">
 * http://www.exampledepot.com/egs/javax.xml.parsers/CreateDom.html</a>.
 * @return an empty <code>Document</code> object
 * @see http://www.exampledepot.com/egs/javax.xml.parsers/CreateDom.html
 */
public static Document createDomDocument() {
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document localDoc = builder.newDocument();
        return localDoc;
    } catch (ParserConfigurationException pce) {
        System.err.println(pce);
    }
    return null;
}