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 createDocument() throws IOException {
    DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {/*  w  w  w.j a va 2  s . co  m*/
        builder = dBF.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IOException("Error creating document builder. (" + e.getMessage() + ")");
    }
    return builder.newDocument();
}

From source file:Main.java

public static Document createXMLDocument() {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);/* w  ww.ja v a2s  . c  om*/
    DocumentBuilder docBuilder = null;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:Main.java

public static Element createElement(String tag) throws Exception {
    DocumentBuilderFactory oDOMFactory = null;
    DocumentBuilder oDOMBuilder = null;
    org.w3c.dom.Document oDOMDocument = null;

    oDOMFactory = DocumentBuilderFactory.newInstance();
    // oDOMFactory.setNamespaceAware(true);
    oDOMBuilder = oDOMFactory.newDocumentBuilder();
    oDOMDocument = oDOMBuilder.newDocument();

    Element oElement = oDOMDocument.createElement(tag);

    return oElement;
}

From source file:Main.java

public static Document newDocument() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {/*from  w  ww  .ja va2  s.com*/
        builder = factory.newDocumentBuilder();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return builder.newDocument();
}

From source file:Main.java

/** Creates an empty XML document. */
public static Document createEmptyDocument() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {/*from   w w w.  j  ava2  s  .  co  m*/
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("No document builder found, probably Java is misconfigured!", e);
    }
    return builder.newDocument();
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static Element feed(final AtomFeed feed) throws ParserConfigurationException {
    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element feedElem = doc.createElement(ODataConstants.ATOM_ELEM_FEED);
    feedElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    feedElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    feedElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    feedElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    feedElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (feed.getBaseURI() != null) {
        feedElem.setAttribute(ODataConstants.ATTR_XMLBASE, feed.getBaseURI().toASCIIString());
    }/*  w w w.j ava2 s .c om*/
    doc.appendChild(feedElem);

    if (StringUtils.isNotBlank(feed.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(feed.getTitle()));
        feedElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(feed.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(feed.getSummary()));
        feedElem.appendChild(summary);
    }

    for (AtomEntry entry : feed.getEntries()) {
        feedElem.appendChild(doc.importNode(entry(entry), true));
    }

    return feedElem;
}

From source file:Main.java

/**
 * Create a new blank XML document.//from   w w w . j a v  a  2  s  .co  m
 * 
 * @return The new blank XML document.
 * 
 * @throws IOException
 *             If there is an error creating the XML document.
 * @throws ParseException
 */
public static Document newDocument() throws ParseException {

    final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = builderFactory.newDocumentBuilder();
    } catch (final ParserConfigurationException e) {
        final ParseException thrown = new ParseException(e.getMessage(), 0);
        throw thrown;
    }
    return builder.newDocument();
}

From source file:jlib.xml.XMLUtil.java

public static Document LoadXML(Source file) {
    try {/*from  w  ww .  j  a v  a2 s . c o m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Result res = new DOMResult(doc);
        TransformerFactory tr = TransformerFactory.newInstance();
        Transformer xformer = tr.newTransformer();
        xformer.transform(file, res);

        return doc;
    } catch (Exception e) {
        String csError = e.toString();
        Log.logImportant(csError);
        Log.logImportant("ERROR while loading XML " + file.toString());
    }
    return null;
}

From source file:jlib.xml.XMLUtil.java

public static Document loadXML(ByteArrayInputStream byteArrayInputStream) {
    try {/*from   ww w.  j av  a 2  s  .co  m*/
        StreamSource streamSource = new StreamSource(byteArrayInputStream);
        DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Result res = new DOMResult(doc);
        TransformerFactory tr = TransformerFactory.newInstance();
        Transformer xformer = tr.newTransformer();
        xformer.transform(streamSource, res);

        return doc;
    } catch (Exception e) {
        String csError = e.toString();
        Log.logImportant(csError);
        Log.logImportant(
                "ERROR while loading XML from byteArrayInputStream " + byteArrayInputStream.toString());
    }
    return null;
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Document newDocument() throws SystemException {
    Document doc = null;//from  w ww  .  j  a  v a  2 s.  c  o  m
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        doc = builder.newDocument();
    } catch (Exception e) {
        log.error("Error creating new XML document", e);
        throw new SystemException("Error creating new XML document", e);
    }

    return doc;
}