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:fr.inria.wimmics.coresetimer.CoreseTimer.java

public void writeStatistics() {
    Document doc = null;/*from  ww w.  ja v a2  s .c  om*/
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.newDocument();
        Element rootElement = doc.createElement("TestResult");

        Element inputs = doc.createElement("Inputs");

        Element inputFile = doc.createElement("Input");
        Text inputFileText = doc.createTextNode(test.getInput());
        inputFile.appendChild(inputFileText);

        Element request = doc.createElement("Request");
        Text requestText = doc.createTextNode(test.getRequest());
        request.appendChild(requestText);

        Element timestamp = doc.createElement("Timestamp");
        Text timestampText = doc.createTextNode(LocalDateTime.now().toString());
        timestamp.appendChild(timestampText);

        Element[] subElements = { inputFile, request, timestamp };
        for (Element e : subElements) {
            inputs.appendChild(e);
        }

        Element outputs = doc.createElement("Statistics");

        Element statsMemory = doc.createElement("CPU");
        Text statsMemoryText = doc.createTextNode(getStats().toString());
        statsMemory.appendChild(statsMemoryText);

        Element statsMemoryCoreseMem = doc.createElement("Memory");
        Text statsMemoryCoreseMemText = doc.createTextNode(getStatsMemory().toString());
        statsMemoryCoreseMem.appendChild(statsMemoryCoreseMemText);

        Element[] subElements2 = { statsMemory, statsMemoryCoreseMem };
        for (Element e : subElements2) {
            outputs.appendChild(e);
        }

        rootElement.appendChild(inputs);
        rootElement.appendChild(outputs);

        doc.appendChild(rootElement);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult streamResult = new StreamResult(new File(test.getOutputPath()));
        transformer.transform(source, streamResult);
        LOGGER.log(Level.INFO, "Results were written in:", test.getOutputPath());
    } catch (ParserConfigurationException | TransformerException ex) {
        LOGGER.log(Level.INFO, "Error when writing results:", ex.getMessage());
        ex.printStackTrace();
    }

}

From source file:org.getwheat.harvest.library.dom.DomHelper.java

/**
 * Creates an empty {@link Document}.//w w  w. ja  va 2  s.com
 * 
 * @return a DOM Document
 * @throws ParserConfigurationException
 */
public Document create() throws ParserConfigurationException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.newDocument();
}

From source file:fr.adfab.magebeans.processes.CreateModuleProcess.java

protected void _createConfigXml() {
    try {/*from  ww w  .j  a v a 2s .  c o  m*/
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        this.dom = documentBuilder.newDocument();
        this.configElement = this.dom.createElement("config");
        this.globalElement = this.dom.createElement("global");

        // Create config for module version
        Element moduleElement = this.dom.createElement(this.moduleName);
        Element versionElement = this.dom.createElement("version");
        versionElement.setTextContent("0.1.0");
        moduleElement.appendChild(versionElement);
        this.configElement.appendChild(moduleElement);

        if (this.hasBlock) {
            this._createConfigNode("block");
            this._createFolders("Block");
        }

        if (this.hasModel) {
            this._createConfigNode("model");
            this._createFolders("Model");
        }

        if (this.hasHelper) {
            this._createConfigNode("helper");
            this._createFolders("Helper");
        }

        if (this.hasSetup) {
            this._createSetup();
        }

        this.configElement.appendChild(this.globalElement);
        this.dom.appendChild(this.configElement);
    } catch (ParserConfigurationException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:fr.ortolang.diffusion.seo.SeoServiceBean.java

private Document generateSiteMapDocument() throws ParserConfigurationException, SeoServiceException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    Element urlset = doc.createElement("urlset");
    urlset.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", SITEMAP_NS_URI);

    String marketServerUrl = OrtolangConfig.getInstance()
            .getProperty(OrtolangConfig.Property.MARKET_SERVER_URL);

    generateMarketSectionEntries(urlset, doc, marketServerUrl);
    generateWorkspacesEntries(urlset, doc, marketServerUrl);

    doc.appendChild(urlset);/* w  ww  . ja va  2s  .c o m*/

    return doc;
}

From source file:hd3gtv.as5kpc.protocol.ProtocolHandler.java

private Document createDocument() {
    try {// w w  w  .j  a va2  s  . c om
        DocumentBuilderFactory fq = DocumentBuilderFactory.newInstance();
        DocumentBuilder constructeur = fq.newDocumentBuilder();
        Document document = constructeur.newDocument();
        document.setXmlVersion("1.0");
        document.setXmlStandalone(true);
        return document;
    } catch (Exception e) {
        log.error("Can't make XML document", e);
    }
    return null;
}

From source file:com.hp.saas.agm.core.entity.EntityQuery.java

public synchronized Element toElement(String targetName) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element element = doc.createElement(targetName);
    if (getName() != null) {
        element.setAttribute("name", getName());
    }/*from  w ww.j  ava2s.  co m*/
    Element filter = super.toElement("filter");
    element.appendChild(filter);
    Element cross = doc.createElement("crossFilter");
    Map<String, EntityCrossFilter> crossFilters = getCrossFilters();
    for (String alias : crossFilters.keySet()) {
        EntityCrossFilter f = crossFilters.get(alias);
        Element crossElement = f.toElement(f.getEntityType());
        if (!alias.equals(f.getEntityType())) {
            crossElement.setAttribute("alias", alias);
        }
        cross.appendChild(crossElement);
    }
    element.appendChild(cross);
    Element orderBy = doc.createElement("order");
    for (String name : order.keySet()) {
        Element child = doc.createElement(name);
        child.setAttribute("dir", order.get(name).name());
        orderBy.appendChild(child);
    }
    element.appendChild(orderBy);
    Element viewing = doc.createElement("view");
    for (String name : view.keySet()) {
        Element child = doc.createElement("column");
        child.setAttribute("name", name);
        child.setAttribute("width", String.valueOf(view.get(name)));
        viewing.appendChild(child);
    }
    element.appendChild(viewing);
    return element;
}

From source file:ch.entwine.weblounge.bridge.oaipmh.util.XmlGen.java

/**
 * Create a new environment.//from ww w  .j  a  v a  2s .  co m
 */
public XmlGen() {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.uzk.hki.da.metadata.MetadataStructure.java

public void toEDM(HashMap<String, HashMap<String, List<String>>> indexInfo, File file,
        PreservationSystem preservationSystem, String objectID, String urn) {
    try {//  ww  w .j ava  2s.co m
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document edmDoc = docBuilder.newDocument();
        Element rootElement = edmDoc.createElement("rdf:RDF");
        edmDoc.appendChild(rootElement);

        addXmlNsToEDM(edmDoc, rootElement);

        for (String id : indexInfo.keySet()) {
            logger.debug("Index information about " + id + ": " + indexInfo.get(id));
            Element providedCHO = addEdmProvidedCHOtoEdm(preservationSystem, id, edmDoc, rootElement);
            Element aggregation = addOreAggregationToEdm(preservationSystem, id, edmDoc, rootElement);

            if (indexInfo.get(id).get(C.EDM_IS_PART_OF) == null) {
                List<String> root = new ArrayList<String>();
                root.add("is root element");
                indexInfo.get(id).put(C.EDM_HAS_TYPE, root);
            }

            if (indexInfo.get(id).get(C.EDM_IDENTIFIER) == null) {
                List<String> IDs = new ArrayList<String>();
                IDs.add(objectID);
                IDs.add(urn);
                indexInfo.get(id).put(C.EDM_IDENTIFIER, IDs);
            } else {
                indexInfo.get(id).get(C.EDM_IDENTIFIER).add(objectID);
                indexInfo.get(id).get(C.EDM_IDENTIFIER).add(urn);
            }

            for (String elementName : indexInfo.get(id).keySet()) {
                Element parentNode = null;
                if (elementName.startsWith("dc:") || elementName.startsWith("dcterms:")
                        || elementName.equals(C.EDM_HAS_TYPE)) {
                    parentNode = providedCHO;
                } else if (elementName.startsWith("edm:")) {
                    parentNode = aggregation;
                }
                if (parentNode != null) {
                    List<String> values = indexInfo.get(id).get(elementName);
                    for (String currentValue : values) {
                        if (!currentValue.equals("")) {
                            addNewElementToParent(preservationSystem, id, elementName, currentValue, parentNode,
                                    edmDoc);
                        }
                    }
                }
            }
        }

        javax.xml.transform.Source source = new javax.xml.transform.dom.DOMSource(edmDoc);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Result result = new javax.xml.transform.stream.StreamResult(file);
        transformer.transform(source, result);

    } catch (Exception e) {
        logger.error("Unable to create the edm file!");
        throw new RuntimeException(e);
    }

}

From source file:net.sf.jabref.exporter.OOCalcDatabase.java

public Document getDOMrepresentation() {
    Document result = null;//from   w  ww . ja  v  a2s .c o m
    try {
        DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        result = dbuild.newDocument();
        Element collection = result.createElement("office:document-content");
        //collection.setAttribute("xmlns", "http://openoffice.org/2000/office");
        collection.setAttribute("xmlns:office", "http://openoffice.org/2000/office");
        collection.setAttribute("xmlns:style", "http://openoffice.org/2000/style");
        collection.setAttribute("xmlns:text", "http://openoffice.org/2000/text");
        collection.setAttribute("xmlns:table", "http://openoffice.org/2000/table");
        collection.setAttribute("xmlns:office:class", "spreadsheet");
        collection.setAttribute("xmlns:office:version", "1.0");
        collection.setAttribute("xmlns:fo", "http://www.w3.org/1999/XSL/Format");
        Element el = result.createElement("office:script");
        collection.appendChild(el);

        el = result.createElement("office:automatic-styles");
        Element el2 = result.createElement("style:style");
        el2.setAttribute("style:name", "ro1");
        el2.setAttribute("style:family", "table-row");
        Element el3 = result.createElement("style.properties");
        el3.setAttribute("style:row-height", "0.1681inch");
        el3.setAttribute("fo:break-before", "auto");
        el3.setAttribute("style:use-optimal-row-height", "true");
        el2.appendChild(el3);
        el.appendChild(el2);
        el2 = result.createElement("style:style");
        el2.setAttribute("style:name", "ta1");
        el2.setAttribute("style:family", "table");
        el2.setAttribute("style:master-page-name", "Default");
        el3 = result.createElement("style:properties");
        el3.setAttribute("table:display", "true");
        el2.appendChild(el3);
        el.appendChild(el2);
        collection.appendChild(el);

        Element body = result.createElement("office:body");
        Element table = result.createElement("table:table");
        table.setAttribute("table:name", "biblio");
        table.setAttribute("table.style-name", "ta1");

        Element row = result.createElement("table:table-row");
        row.setAttribute("table.style-name", "ro1");
        addTableCell(result, row, "Type");
        addTableCell(result, row, "ISBN");
        addTableCell(result, row, "Identifier");
        addTableCell(result, row, "Author");
        addTableCell(result, row, "Title");
        addTableCell(result, row, "Journal");
        addTableCell(result, row, "Volume");
        addTableCell(result, row, "Number");
        addTableCell(result, row, "Month");
        addTableCell(result, row, "Pages");
        addTableCell(result, row, "Year");
        addTableCell(result, row, "Address");
        addTableCell(result, row, "Note");
        addTableCell(result, row, "URL");
        addTableCell(result, row, "Booktitle");
        addTableCell(result, row, "Chapter");
        addTableCell(result, row, "Edition");
        addTableCell(result, row, "Series");
        addTableCell(result, row, "Editor");
        addTableCell(result, row, "Publisher");
        addTableCell(result, row, "ReportType");
        addTableCell(result, row, "Howpublished");
        addTableCell(result, row, "Institution");
        addTableCell(result, row, "Organization");
        addTableCell(result, row, "School");
        addTableCell(result, row, "Annote");
        addTableCell(result, row, "Assignee");
        addTableCell(result, row, "Day");
        addTableCell(result, row, "Dayfiled");
        addTableCell(result, row, "Monthfiled");
        addTableCell(result, row, "Yearfiled");
        addTableCell(result, row, "Language");
        addTableCell(result, row, "Nationality");
        addTableCell(result, row, "Revision");
        addTableCell(result, row, "Custom1");
        addTableCell(result, row, "Custom2");
        addTableCell(result, row, "Custom3");
        addTableCell(result, row, "Custom4");
        addTableCell(result, row, "Custom5");
        table.appendChild(row);

        for (BibEntry e : entries) {
            row = result.createElement("table:table-row");
            addTableCell(result, row, new GetOpenOfficeType().format(e.getType()));
            addTableCell(result, row, getField(e, "isbn"));
            addTableCell(result, row, getField(e, BibEntry.KEY_FIELD));
            addTableCell(result, row, getField(e, "author"));//new AuthorLastFirst().format(getField(e, "author")));
            addTableCell(result, row,
                    new RemoveWhitespace().format(new RemoveBrackets().format(getField(e, "title"))));
            addTableCell(result, row, getField(e, "journal"));
            addTableCell(result, row, getField(e, "volume"));
            addTableCell(result, row, getField(e, "number"));
            addTableCell(result, row, getField(e, "month"));
            addTableCell(result, row, getField(e, "pages"));
            addTableCell(result, row, getField(e, "year"));
            addTableCell(result, row, getField(e, "address"));
            addTableCell(result, row, getField(e, "note"));
            addTableCell(result, row, getField(e, "url"));
            addTableCell(result, row, getField(e, "booktitle"));
            addTableCell(result, row, getField(e, "chapter"));
            addTableCell(result, row, getField(e, "edition"));
            addTableCell(result, row, getField(e, "series"));
            addTableCell(result, row, getField(e, "editor"));//new AuthorLastFirst().format(getField(e, "editor")));
            addTableCell(result, row, getField(e, "publisher"));
            addTableCell(result, row, getField(e, "reporttype"));
            addTableCell(result, row, getField(e, "howpublished"));
            addTableCell(result, row, getField(e, "institution"));
            addTableCell(result, row, getField(e, "organization"));
            addTableCell(result, row, getField(e, "school"));
            addTableCell(result, row, getField(e, "annote"));
            addTableCell(result, row, getField(e, "assignee"));
            addTableCell(result, row, getField(e, "day"));
            addTableCell(result, row, getField(e, "dayfiled"));
            addTableCell(result, row, getField(e, "monthfiled"));
            addTableCell(result, row, getField(e, "yearfiled"));
            addTableCell(result, row, getField(e, "language"));
            addTableCell(result, row, getField(e, "nationality"));
            addTableCell(result, row, getField(e, "revision"));
            addTableCell(result, row, "");
            addTableCell(result, row, "");
            addTableCell(result, row, "");
            addTableCell(result, row, "");
            addTableCell(result, row, "");
            table.appendChild(row);
        }

        body.appendChild(table);
        collection.appendChild(body);

        result.appendChild(collection);
    } catch (Exception e) {
        LOGGER.warn("Exception caught...", e);
    }
    return result;
}

From source file:net.sf.jabref.logic.mods.MODSEntry.java

private Node getDOMrepresentation() {
    Node result;/*  www . j a  va  2  s.  com*/
    try {
        DocumentBuilder d = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        result = getDOMrepresentation(d.newDocument());
    } catch (Exception e) {
        throw new Error(e);
    }
    return result;
}