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 Document xml2doc(InputStream inputStream)
        throws IOException, SAXException, ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
}

From source file:Main.java

/**
 * Creates a new XML document./*from   w w  w  .  j av a2 s.co m*/
 * @return the XML document
 */
public static Document createDocument() {
    try {
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = fact.newDocumentBuilder();
        return db.newDocument();
    } catch (ParserConfigurationException e) {
        //will probably never be thrown because we're not doing anything fancy with the configuration
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Element createElement(String tag) throws Exception {
    DocumentBuilderFactory oDOMFactory = null;
    DocumentBuilder oDOMBuilder = null;
    org.w3c.dom.Document oDOMDocument = null;

    oDOMFactory = DocumentBuilderFactory.newInstance();
    // oDOMFactory.setNamespaceAware(true);
    oDOMBuilder = oDOMFactory.newDocumentBuilder();
    oDOMDocument = oDOMBuilder.newDocument();

    Element oElement = oDOMDocument.createElement(tag);

    return oElement;
}

From source file:Main.java

private synchronized static void initializeDocumentBuilder() throws Exception {
    if (documentBuilder == null) {
        documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    }//from  w ww. j  a va  2 s. co m

}

From source file:Main.java

/**
 * Creates a new XML document./* ww w.j  a  va 2  s  .co m*/
 * @return the XML document
 */
public static Document createDocument() {
    try {
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        fact.setNamespaceAware(true);
        DocumentBuilder db = fact.newDocumentBuilder();
        return db.newDocument();
    } catch (ParserConfigurationException e) {
        //will probably never be thrown because we're not doing anything fancy with the configuration
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w  w w  .  java  2s  .  c  o m
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

public static Document getDocument(File file) throws Throwable {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(file);
    return doc;//from   www.  j  a  v a  2  s.  com
}

From source file:Main.java

public static Element getroot(String siteName) {
    Element root = null;//from ww w . j a va 2s  .co m
    try {
        File fXmlFile = new File(siteName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.parse(fXmlFile);
        root = document.getDocumentElement();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return root;

}

From source file:Main.java

public static Document getXMLDoc(String root) throws ParserConfigurationException {
    Document xmldoc = null;/*ww w.  j a v  a2 s.c  om*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    // Document.
    xmldoc = impl.createDocument(null, root, null);
    return xmldoc;
}

From source file:Main.java

public static void ReadXMLFile2() {

    try {/*from  www  .j  a  v a  2s . c  om*/

        File file = new File("D:\\FAR_Documents\\__Startamap\\Home.xml");

        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Document doc = dBuilder.parse(file);

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        if (doc.hasChildNodes()) {

            printNote(doc.getChildNodes());

        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}