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 parse(String s) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new ErrorHandler() {
        @Override/*w  w w .  j a  va2s  .  c om*/
        public void warning(SAXParseException exception) throws SAXException {

        }

        @Override
        public void error(SAXParseException exception) throws SAXException {

        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {

        }
    });
    Document rv = builder.parse(new ByteArrayInputStream(s.getBytes("UTF-8")));
    return rv;
}

From source file:Main.java

public static Document createDocument() {
    Document document = null;/*w w  w .  j a  v  a2  s.  c  o  m*/

    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.newDocument();
    } catch (ParserConfigurationException ex) {
        // Ignore parser configuration errors
    }

    return document;
}

From source file:Main.java

public static Document stringToDocument(String xml) {
    Document doc = null;//from  w ww  .ja  v a  2  s  .  c o m
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Turn on validation, and turn off namespaces
    factory.setValidating(false);
    factory.setNamespaceAware(false);

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Main.java

static public Document getNewDocument() throws Exception {

    //Create instance of DocumentBuilderFactory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //Get the DocumentBuilder
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    //Create blank DOM Document
    Document doc = docBuilder.newDocument();
    return doc;//  ww w.ja  v  a  2s .  c  o  m
}

From source file:Main.java

public static Document buildAndParseDocument(String in) {
    try {/*from w ww.  j a  v  a 2 s  . co  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document getDocumentFromString(String xmlString) throws Exception {
    DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {/*from   w  w  w.  ja va  2 s . c o m*/
        // Do not load the DTD
        oDocumentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
                Boolean.FALSE);
    } catch (IllegalArgumentException e) {
    }
    DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();

    InputSource inputSource = new InputSource(new StringReader(xmlString));
    return oDocumentBuilder.parse(inputSource);
}

From source file:Main.java

private static DocumentBuilder setUpDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    return documentBuilderFactory.newDocumentBuilder();
}

From source file:Main.java

public static Document convertStringToDocument(String domStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;/*  w w w.jav  a  2 s.c o m*/
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(domStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static public Document inputStreamToDocument(InputStream inputStream) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(inputStream);
    doc.getDocumentElement().normalize();

    return doc;/*from  ww  w.jav a  2  s.  co m*/
}

From source file:Main.java

public static Document newDocument() {
    try {/*from ww w .j av a2s  .  c  o  m*/
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}