Example usage for javax.xml.parsers DocumentBuilderFactory newInstance

List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newInstance.

Prototype

public static DocumentBuilderFactory newInstance() 

Source Link

Document

Obtain a new instance of a DocumentBuilderFactory .

Usage

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);/*from  www  .j a v  a2s .c o  m*/
    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

/**
 * Creates an empty DOM Document.//from ww  w. j a va  2s  . 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

/**
 * Creates an empty XML Document./*w  w  w  . j  a  va  2  s  .  c  om*/
 * @return A empty XML document
 * @throws ParserConfigurationException If creation fails because of parser configuratino.
 */
public static Document newDocument() throws ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
}

From source file:Main.java

public static Document getXML(File file) throws Exception {
    InputStream in = new FileInputStream(file);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
    Document xml = docBuilder.parse(in);
    in.close();//from  ww w  . j  av a2  s .  c om
    return xml;
}

From source file:Main.java

public static Document loadDocument(String file)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(file);
}

From source file:Main.java

public static Document getDocumentForStream(InputStream stream) {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document doc = null;//ww w.  j  ava2  s. c o m
    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
        doc = docBuilder.parse(stream);
        doc.getDocumentElement().normalize();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return doc;
}

From source file:Main.java

public static Document xmlFromString(String xml) {
    Document doc;/*from w w  w.  j  a  va  2s. c o m*/

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

From source file:Main.java

private static Document parseXml(String xmlPath) {
    Document doc = null;//from  w ww . j  a  v a 2 s.c o  m
    try {
        File trainSchedule = new File(xmlPath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(trainSchedule);
        doc.getDocumentElement().normalize();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (doc == null)
        throw new ParserException("cannot parse: \"" + xmlPath + "\"");

    return doc;
}

From source file:Main.java

/**
 * Create a document which is used to append XML nodes
 *
 * @return/*  ww w .j  a va2  s  . co  m*/
 */
public static Document getNewDocument() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        return doc;
    } catch (ParserConfigurationException e) {
    }
    return null;
}

From source file:Main.java

/**
 * Creates a document/*from w  w w .  j  a  v a  2s  .co m*/
 * @return Document
 * @throws ParserConfigurationException
 */
public static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    return document;
}