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

static public Document newDocument() {
    Document document = null;/*  www .  ja  v a  2 s .co m*/
    DocumentBuilder builder = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        builder = factory.newDocumentBuilder();
        document = builder.newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

/**
 * Generates a new document using the XML factory builder stuff.
 *
 * @return the new Document, never supposed to be null
 *
 * @throws ParserConfigurationException if a dom configuration error
 *                                      occurs.
 *//*from w w  w.  ja  v  a2 s.  co  m*/
public static Document createDocument() throws ParserConfigurationException {
    final DocumentBuilderFactory factory;
    final DocumentBuilder builder;
    final Document document;
    factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();
    document = builder.newDocument();
    return document;
}

From source file:Main.java

public static Document novoDocument() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from  w  ww  .jav  a 2s.  com*/
    DocumentBuilder builder = null;
    try {
        builder = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }

    return builder.newDocument();
}

From source file:Main.java

public static Document createDocument() {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    try {/*from   www . ja va2  s .co  m*/
        docBuilder = dbfac.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:Main.java

static void initializeXMLReport(int numThreads, int experiment, int sampleInterval, String managerClassName,
        String benchmarkClassName, String adapterClassName) {
    try {/*from  w ww .ja v  a 2 s .com*/
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = builder.newDocument();

        Element root = doc.createElement("Statistics");
        doc.appendChild(root);

        Element element = doc.createElement("Benchmark");
        element.setTextContent(benchmarkClassName);
        root.appendChild(element);

        element = doc.createElement("Adapter");
        element.setTextContent(adapterClassName);
        root.appendChild(element);

        element = doc.createElement("ContentionManager");
        element.setTextContent(managerClassName);
        root.appendChild(element);

        element = doc.createElement("Threads");
        element.setTextContent(Integer.toString(numThreads));
        root.appendChild(element);

        element = doc.createElement("Mix");
        element.setTextContent(Integer.toString(experiment));
        root.appendChild(element);

        element = doc.createElement("SampleInterval");
        element.setTextContent(Long.toString(sampleInterval));
        root.appendChild(element);

        String name = System.getProperty("user.name");
        if (name == null)
            name = "";
        element = doc.createElement("Owner");
        element.setTextContent(name);
        root.appendChild(element);

        java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getDefault());
        String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(java.util.TimeZone.getDefault());
        element = doc.createElement("Date");
        element.setTextContent(sdf.format(cal.getTime()));
        root.appendChild(element);

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * @return New XML document from the default document builder factory.
 *//*from   ww  w.  j  a  v  a  2  s . com*/
private static Document createDocument() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return builder.newDocument();
}

From source file:Main.java

public static Document getEmptyDocument() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {//from   w  w  w .ja va 2 s . c  om
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        return null;
    }
    return documentBuilder.newDocument();
}

From source file:Main.java

public static Document createDocumentNode() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;

    try {//from   w  w  w  .  ja va  2  s .co  m
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    return builder.newDocument();
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TaxonomyUtils.java

public static String checkScientificNameAtNCBI(String scientificName) {
    try {/*from  w ww . j av a 2  s  .  c om*/
        String query = ncbiEntrezUtilsURL + "db=taxonomy&term=" + URLEncoder.encode(scientificName, "UTF-8");
        final HttpClient httpclient = HttpClientBuilder.create().build();
        HttpGet httpget = new HttpGet(query);
        try {
            HttpResponse response = httpclient.execute(httpget);
            String out = parseEntity(response.getEntity());
            log.info(out);
            try {
                DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document d = docBuilder.newDocument();

                TransformerFactory.newInstance().newTransformer()
                        .transform(new StreamSource(new UnicodeReader(out)), new DOMResult(d));

                NodeList nl = d.getElementsByTagName("Id");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    return e.getTextContent();
                }
            } catch (ParserConfigurationException e) {
                log.error("check scientific name at NCBI", e);
            } catch (TransformerException e) {
                log.error("check scientific name at NCBI", e);
            }
        } catch (ClientProtocolException e) {
            log.error("check scientific name at NCBI", e);
        } catch (IOException e) {
            log.error("check scientific name at NCBI", e);
        }
    } catch (UnsupportedEncodingException e) {
        log.error("check scientific name at NCBI", e);
    }
    return null;
}

From source file:Main.java

/**
 * Converts an XML String to a Document.
 *
 * @param string//from   w w w.  j  a  va  2s .c om
 * @return
 * @throws TransformerException
 * @throws ParserConfigurationException
 */
public static Document toDocument(String string) throws TransformerException, ParserConfigurationException {

    // if there is a byte order mark, strip it off.
    // otherwise, we get a org.xml.sax.SAXParseException: Content is not allowed in prolog
    if (string.startsWith("\uFEFF")) {
        string = string.substring(1);
    }

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // StringReader source
    Source source = new StreamSource(new StringReader(string));

    // Document result
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();
    Result result = new DOMResult(document);

    transformer.transform(source, result);
    return document;
}