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

/**
 * Convenience method to create new XML DOM Document.
 *
 * @return a new instance of a DOM {@link Document}.
 * @throws ParserConfigurationException in case of a problem retrieving {@link DocumentBuilder}.
 *///from ww w  . j av a2s  .  co  m
public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

public static Document documentFromNode(Element element) {
    try {//w  ww.  j a  va 2 s.c  om
        DocumentBuilder dBuilder = getDocumentBuilder();
        Document newDocument = dBuilder.newDocument();
        Node importedNode = newDocument.importNode(element, true);
        newDocument.appendChild(importedNode);
        return newDocument;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document getDOMMaker() throws ParserConfigurationException {
    //get an instance of factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //get an instance of builder
    DocumentBuilder db = dbf.newDocumentBuilder();
    //create an instance of DOM
    Document dom = db.newDocument();
    return dom;/*from ww  w . j a v  a  2 s  .c o  m*/
}

From source file:Main.java

public static Document createNewDocument() throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();

    return document;
}

From source file:Main.java

/**
 * @return A new document to populate/* www .  j  a v  a2  s  .c  o m*/
 */
public static Document newDocument() {
    ensureFactory();
    try {
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML DOM document
 *
 * @param jaxbElement//from   ww w  .ja  va2  s . co  m
 *            The root of content tree to be marshalled
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document doMarshallingJAXBObject(Object jaxbElement) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    Document doc = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
        marshaller.marshal(jaxbElement, doc);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return doc;
}

From source file:Main.java

private static Document getDoc() throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    //root elements
    Document doc = docBuilder.newDocument();
    return doc;//from w ww.j  ava 2s.  c o m
}

From source file:Main.java

/**
 * Creates an empty DOM document//from   w w  w .j  av a2s. co  m
 * @return
 */
public static Document createDomDocument() {
    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        document = documentBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Cannot create DOM document", e);
    }
    return document;
}

From source file:Main.java

private static Element constructDocument() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    Element rootElement = doc.createElement("root");
    doc.appendChild(rootElement);// www  . ja  v  a 2 s  . c om
    Element c1Element = doc.createElement("c1");
    Element c2Element = doc.createElement("c2");
    Element c3Element = doc.createElement("c3");
    Element gc1Element = doc.createElement("gc1_1");
    Element gc2Element = doc.createElement("gc1_2");
    Text textNode = doc.createTextNode("uncle_freddie");
    Element gc3Element = doc.createElement("gc2_1");

    rootElement.appendChild(c1Element);
    rootElement.appendChild(c2Element);
    rootElement.appendChild(c3Element);

    c1Element.appendChild(gc1Element);
    c1Element.appendChild(gc2Element);
    c2Element.appendChild(gc3Element);

    gc3Element.appendChild(textNode);

    return rootElement;
}

From source file:Main.java

/**
 * Returns a new, empty DOM document./*from   w ww . ja  va2s  .  c o  m*/
 * 
 * @return Returns a new DOM document.
 */
public static Document createDocument() {
    Document result = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();

        result = parser.newDocument();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return result;
}