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:Utils.java

public static Document newEmptyDocument() {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret;//  w w  w  . j a va  2s .c  om

    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    ret = builder.newDocument();

    return ret;
}

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

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

    final Element entryElem = doc.createElement(ODataConstants.ATOM_ELEM_ENTRY);
    entryElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    entryElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    entryElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    entryElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    entryElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (entry.getBaseURI() != null) {
        entryElem.setAttribute(ODataConstants.ATTR_XMLBASE, entry.getBaseURI().toASCIIString());
    }/*from w  ww  .j av  a2 s . c o  m*/
    doc.appendChild(entryElem);

    final Element category = doc.createElement(ODataConstants.ATOM_ELEM_CATEGORY);
    category.setAttribute(ODataConstants.ATOM_ATTR_TERM, entry.getType());
    category.setAttribute(ODataConstants.ATOM_ATTR_SCHEME, ODataConstants.ATOM_CATEGORY_SCHEME);
    entryElem.appendChild(category);

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

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

    setLinks(entryElem, entry.getAssociationLinks());
    setLinks(entryElem, entry.getNavigationLinks());
    setLinks(entryElem, entry.getMediaEditLinks());

    final Element content = doc.createElement(ODataConstants.ATOM_ELEM_CONTENT);
    if (entry.isMediaEntry()) {
        if (StringUtils.isNotBlank(entry.getMediaContentType())) {
            content.setAttribute(ODataConstants.ATTR_TYPE, entry.getMediaContentType());
        }
        if (StringUtils.isNotBlank(entry.getMediaContentSource())) {
            content.setAttribute(ODataConstants.ATOM_ATTR_SRC, entry.getMediaContentSource());
        }
        if (content.getAttributes().getLength() > 0) {
            entryElem.appendChild(content);
        }

        if (entry.getMediaEntryProperties() != null) {
            entryElem.appendChild(doc.importNode(entry.getMediaEntryProperties(), true));
        }
    } else {
        content.setAttribute(ODataConstants.ATTR_TYPE, ContentType.APPLICATION_XML.getMimeType());
        if (entry.getContent() != null) {
            content.appendChild(doc.importNode(entry.getContent(), true));
        }
        entryElem.appendChild(content);
    }

    return entryElem;
}

From source file:com.msopentech.odatajclient.engine.data.Serializer.java

private static void xmlLink(final ODataLink link, final Writer writer) {
    try {//from  w  w w . ja  v  a  2  s.c om
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document doc = builder.newDocument();
        final Element uri = doc.createElementNS(ODataConstants.NS_DATASERVICES, ODataConstants.ELEM_URI);
        uri.appendChild(doc.createTextNode(link.getLink().toASCIIString()));

        dom(uri, writer);
    } catch (Exception e) {
        throw new IllegalArgumentException("While serializing XML link", e);
    }
}

From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document./*from ww w.j  a v a 2  s  .  c o  m*/
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 *
 * @return The Dublin Core Document.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static Document createDublinCoreDocument(DigitalObject theObject, UserData pCreator)
        throws ParserConfigurationException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element root = doc.createElementNS("http://www.openarchives.org/OAI/2.0/oai_dc/", "oai_dc:dc");
    root.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("xsi:schemaLocation",
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    doc.appendChild(root);

    Element title = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:title");
    title.setTextContent(StringEscapeUtils.escapeXml11(theObject.getLabel()));
    root.appendChild(title);

    if (pCreator != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    } else if (theObject.getUploader() != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    }

    for (UserData experimenter : theObject.getExperimenters()) {
        //don't list uploader a second time here
        if (theObject.getUploader() == null || !experimenter.equals(theObject.getUploader())) {
            Element contributor = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:contributor");
            contributor.setTextContent(StringEscapeUtils.escapeXml11(experimenter.getFullname()));
            root.appendChild(contributor);
        }
    }

    if (theObject.getInvestigation() != null) {
        Element subject = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:subject");
        subject.setTextContent(StringEscapeUtils.escapeXml11(theObject.getInvestigation().getTopic()));
        root.appendChild(subject);
        if (theObject.getInvestigation().getDescription() != null) {
            Element description = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:description");
            description.setTextContent(
                    StringEscapeUtils.escapeXml11(theObject.getInvestigation().getDescription()));
            root.appendChild(description);
        }

        if (theObject.getInvestigation().getStudy() != null) {
            if (theObject.getInvestigation().getStudy().getLegalNote() != null) {
                Element rights = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:rights");
                rights.setTextContent(
                        StringEscapeUtils.escapeXml11(theObject.getInvestigation().getStudy().getLegalNote()));
                root.appendChild(rights);
            }
        }
    }

    if (theObject.getStartDate() != null) {
        Element date = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:date");
        date.setTextContent(df.format(theObject.getStartDate()));
        root.appendChild(date);
    }
    Element format = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:format");
    format.setTextContent("application/octet-stream");
    root.appendChild(format);
    Element type = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:type");
    type.setTextContent("Dataset");
    root.appendChild(type);
    Element identifier = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:identifier");
    identifier.setTextContent(
            StringEscapeUtils.escapeXml11(theObject.getDigitalObjectId().getStringRepresentation()));
    root.appendChild(identifier);
    return doc;
}

From source file:de.akra.idocit.wsdl.services.DocumentationGenerator.java

/**
 * Creates a documentation {@link Element} out of <code>docparts</code>. It sets the
 * attributes and adds the addressee elements.
 * /*from  ww w . j av  a 2  s  .c  o m*/
 * Please note: if the parameter <code>thematicGridName</code> is <code>null</code>,
 * no thematicgrid-element will be added to the documentation-element. In this case
 * only the docparts will appear.
 * 
 * @param tagName
 *            Qualified tag name for the documentation element.
 * @param docparts
 *            The documentation parts that should be written into a documentation
 *            element.
 * @return The generated documentation {@link Element} or <code>null</code> if
 *         <code>docparts != null && !docparts.isEmpty()</code>.
 */
public static Element generateDocumentationElement(String tagName, List<Documentation> docparts,
        String thematicGridName) {
    Element docElem = null;
    if (docparts != null && !docparts.isEmpty()) {
        try {
            if (domDocument == null) {
                // must be initialized here, because of error handling
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder;
                builder = factory.newDocumentBuilder();
                domDocument = builder.newDocument();
            }
            docElem = domDocument.createElement(tagName);

            if (thematicGridName != null) {
                // Create Thematic Grid Element
                Element thematicGrid = domDocument.createElement(DocumentationParser.THEMATIC_GRID_TAG_NAME);
                thematicGrid.setAttribute(DocumentationParser.THEMATIC_GRID_ATTRIBUTE_NAME, thematicGridName);
                docElem.appendChild(thematicGrid);

                for (Documentation doc : docparts) {
                    thematicGrid.appendChild(generateDocpartElement(doc));
                }
            } else {
                for (Documentation doc : docparts) {
                    docElem.appendChild(generateDocpartElement(doc));
                }
            }
        } catch (ParserConfigurationException e) {
            logger.log(Level.SEVERE, "This error should not occur.", e);
        }
    }

    return docElem;
}

From source file:Main.java

/**
 * Create a new XML Document.//  w  w  w  .  jav a 2s. c om
 * @param docBuilder DocumentBuilder. Setting this to null will create a new DocumentBuilder.
 * @return Document
 */
public static final Document createNewDocument(DocumentBuilder docBuilder) throws ParserConfigurationException {
    if (docBuilder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        docBuilder = factory.newDocumentBuilder();
    }
    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:elaborate.editor.export.tei.TeiMaker.java

static Document createTeiDocument() {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    try {//w  w  w . j a  v a2s.c o  m
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document teiDocument = docBuilder.newDocument();
        return teiDocument;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java

public static Document convertToDOM(TWorkItemBean documentItem, Integer personID, Locale locale) {
    Document dom = null;/*w  w w  . ja v  a2 s. c  o  m*/
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        dom = builder.newDocument();
    } catch (FactoryConfigurationError e) {
        LOGGER.error("Creating the DOM document failed with FactoryConfigurationError:" + e.getMessage());
        return null;
    } catch (ParserConfigurationException e) {
        LOGGER.error("Creating the DOM document failed with ParserConfigurationException: " + e.getMessage());
        return null;
    }
    Element root = dom.createElement(CUSTOM_XML_ELEMENTS.MAIN_ELEMENT);
    if (documentItem != null) {
        Integer itemID = documentItem.getObjectID();
        List<TWorkItemBean> itemList = new LinkedList<TWorkItemBean>();
        itemList.add(documentItem);
        List<ReportBean> reportBeansList = LoadItemIDListItems.getReportBeansByWorkItems(itemList, personID,
                locale, true, false, false, false, false, false, false, false, false);
        ReportBean showableWorkItem = reportBeansList.get(0);
        Map<Integer, String> showValuesMap = showableWorkItem.getShowValuesMap();
        if (showValuesMap != null) {
            List<TFieldBean> fieldBeansList = FieldBL.loadAll();
            for (TFieldBean fieldBean : fieldBeansList) {
                Integer fieldID = fieldBean.getObjectID();
                String fieldName = fieldBean.getName();
                String showValue = showValuesMap.get(fieldID);
                if (showValue != null && !"".equals(showValue)) {
                    IFieldTypeRT fieldTypeRT = FieldTypeManager.getFieldTypeRT(fieldID);
                    if (fieldTypeRT != null) {
                        if (fieldTypeRT.isLong()) {
                            showValue = StringEscapeUtils.escapeHtml4(showValue);
                        }
                    }
                    appendChild(root, fieldName, showValue, dom);
                    appendChild(root, fieldName + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX, Boolean.TRUE.toString(),
                            dom);
                } else {
                    appendChild(root, fieldName + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX, Boolean.FALSE.toString(),
                            dom);
                    appendChild(root, fieldName, "", dom);
                }
            }

            List<TPersonBean> consultedPersonBeans = PersonBL.getDirectConsultants(itemID);
            addWatcherNodes(consultedPersonBeans, root, dom, CUSTOM_XML_ELEMENTS.CONSULTED_LIST,
                    CUSTOM_XML_ELEMENTS.CONSULTED_PERSON);
            List<TPersonBean> informedPersonBeans = PersonBL.getDirectInformants(itemID);
            addWatcherNodes(informedPersonBeans, root, dom, CUSTOM_XML_ELEMENTS.INFORMED_LIST,
                    CUSTOM_XML_ELEMENTS.INFORMED_PERSON);

            List<HistoryValues> comments = HistoryLoaderBL.getRestrictedWorkItemComments(personID, itemID,
                    locale, false, /*LONG_TEXT_TYPE.ISPLAIN*/ LONG_TEXT_TYPE.ISFULLHTML);
            addCommentNodes(comments, root, dom, locale);

        }
    }
    dom.appendChild(root);
    return dom;
}

From source file:io.selendroid.server.model.internal.JsonXmlUtil.java

public static Document buildXmlDocument(JSONObject tree) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {//  ww  w  .jav a  2s.  co m
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        SelendroidLogger.error("Failed to create documentBuilder", e);
        throw new RuntimeException(e);
    }
    Document document = builder.newDocument();

    // document.setXMLEncoding("UTF-8");
    Element root = document.createElement("views");
    document.appendChild(root);
    buildXmlNode(tree, root, document);
    return document;
}

From source file:com.viettel.ws.client.JDBCUtil.java

/**
 * Create Empty Document//from   w ww  .ja  v  a 2s  .  c o  m
 *
 * @return A empty document
 * @throws ParserConfigurationException - If error when create document
 */
public static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setFeature(FEATURE_GENERAL_ENTITIES, false);
    factory.setFeature(FEATURE_PARAMETER_ENTITIES, false);
    factory.setXIncludeAware(false);
    factory.setExpandEntityReferences(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element results = doc.createElement("Results");
    doc.appendChild(results);
    return doc;
}