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

public static synchronized Document getDocument() throws Exception {
    // System.out.println(filepath);
    return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(filepath);

}

From source file:Main.java

public static Document load(String file) {
    try {//  w  w  w. j  av  a  2s. co  m
        Document document = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        dbf.setNamespaceAware(false);
        DocumentBuilder builder = dbf.newDocumentBuilder();
        document = builder.parse(new File(file));
        document.normalize();
        return document;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static DocumentBuilderFactory getDBF(final boolean namespaceaware) {
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setValidating(false);/*ww w .  j  a v a2 s  .  c o  m*/
    fac.setNamespaceAware(namespaceaware);
    fac.setXIncludeAware(true);

    return fac;
}

From source file:Main.java

public static Document fileToDocument(String filename) throws Exception {
    System.out.println("[xml] read document from '" + filename + "'.");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*  w  ww .  j av  a  2 s  .  co m*/
    DocumentBuilder db = factory.newDocumentBuilder();
    try (FileInputStream sr = new FileInputStream(filename)) {
        Document document = db.parse(sr);
        return document;
    }

}

From source file:Main.java

public static Document stringToDOM(String xmlString) {
    try {//  w w  w.  j  a  v a 2  s .  c o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (Throwable t) {
        throw new RuntimeException("Error while building document");
    }
}

From source file:Main.java

/**
 *
 * @return a brand new {@link Document} instance.
 *//*from   ww w  . ja  va2s  .c om*/
public static Document newDocument() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (final ParserConfigurationException e) {
        throw new RuntimeException(e); // TODO proper exception handling
    }
    return builder.newDocument();
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    if (docBuilder == null) {
        docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } else {/*from w w  w.  j  a v  a 2 s.c om*/
        docBuilder.reset();
    }
    return docBuilder;
}

From source file:Main.java

/**
 * Create blank Document./*www . ja  va  2s .  com*/
 */
public final static Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.newDocument();
}

From source file:Main.java

public static Object getClassFromXML() throws Exception {
    //create DOM object
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("property.xml"));

    //get the node name and get the class name
    NodeList nodeList = document.getElementsByTagName("className");
    Node node = nodeList.item(0).getFirstChild();
    String className = node.getNodeValue();

    //return an object through the class name
    Class tempClass = Class.forName(className);
    Object object = tempClass.newInstance();
    return object;
}

From source file:Main.java

public static Document getDocument(String xml) {
    if (xml == null || xml.indexOf("<?xml") < 0)
        return null;
    try {/*from  w ww . ja v  a2 s . c o  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}