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 addXMLItems(File xmlfile, String tag) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc;//from w  w w  .j  av  a 2 s.  co  m
    if (xmlfile.length() == 0) {
        doc = builder.newDocument();
        Element rootElement = doc.createElement(tag + "s");
        doc.appendChild(rootElement);
    } else {
        doc = builder.parse(xmlfile);
    }
    return doc;
}

From source file:Main.java

public static Document createDocument() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    Document document = null;//from   w  w  w.j  a  v  a  2 s . com
    try {
        db = dbf.newDocumentBuilder();
        document = db.newDocument();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return document;
}

From source file:Main.java

public static void createXML222(String xmlFile, String xpath, String element, String data) {
    try {/*  ww w . j a v a  2  s  . c  om*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element rootElement = document.createElement(xpath);
        document.appendChild(rootElement);
        for (int i = 1; i <= 1; i++) {
            Element em = document.createElement(element);
            em.appendChild(document.createTextNode(data));
            rootElement.appendChild(em);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates an empty DOM Document.//  w  w  w. j a v  a 2  s  . c  om
 * @return Document - the created document
 */
public static Document createEmptyDocument() {
    DocumentBuilder builder;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void createXMLDocument(File file, String rootName) {
    if (!file.exists()) {
        try {/*from w  w w  . ja  va2 s.c  o m*/
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            Element root = document.createElement(rootName);
            document.appendChild(root);
            saveXMLDocument(document, file);
        } catch (Exception ex) {
        }
    }
}

From source file:Main.java

public static Node createDocument(String rootElementName) {
    try {/*from  w w w  .  j  a  va2s  .c  o  m*/
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();

        Element rootElement = doc.createElement(rootElementName);
        doc.appendChild(rootElement);

        return rootElement;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Create a new DOM document./*from w w  w .ja v  a 2s.com*/
 * 
 * @return an empty DOM Document
 */
public static Document createDomDocument() {
    try {
        final DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
        final Document doc = builder.newDocument();
        return doc;
    } catch (final ParserConfigurationException e) {
    }
    return null;
}

From source file:Main.java

public static String transformXmlToString(Document doc, String encoding)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();

    Document document = builder.newDocument();

    Element e = doc.getDocumentElement();
    Node n = document.importNode(e, true);
    document.appendChild(n);//  ww  w . j  av  a2 s.  c  o  m

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);

    StringWriter sw = new StringWriter();

    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);

    return sw.toString();
}

From source file:Main.java

/**
 * Creates an empty JAXP XML document.//ww w.  ja va  2 s . c o  m
 * 
 * @return An empty JAXP XML document.
 */
public static Document getNewEmptyJAXPDoc() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder;

    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        return documentBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Document startXMLDocument(String _firstTag) {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document doc = null;/*w ww  .  j  av  a  2s  .c o  m*/
    try {
        docBuilder = dbfac.newDocumentBuilder();
        doc = docBuilder.newDocument();

        Element xmlRoot = doc.createElement(_firstTag);
        doc.appendChild(xmlRoot);

    } catch (ParserConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return doc;
}