Example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

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

Introduction

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

Prototype


public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;

Source Link

Document

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

Usage

From source file:Main.java

public static Document read(InputStream input) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from   ww w  . j a  va2s  .c  om*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.parse(input);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // SAXReader saxReader = new SAXReader();
    // Document document;
    // try {
    // document = saxReader.read(input);
    // }
    // catch (DocumentException e) {
    // throw new RuntimeException(e.getMessage(), e);
    // }
    // return document;
}

From source file:Main.java

public static Document parseToDocument(InputStream is, boolean debugModeEnabled) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(is);

    if (debugModeEnabled)
        System.out.println("Body:\n" + serializeDocument(document) + "\n");

    return document;
}

From source file:Main.java

/**
 * creates a new empty DOM document./* www .j a va  2  s  . co m*/
 *
 * @return                  Document
 */
public static Document createDocument() {
    Document retDocument = null;
    try {
        DocumentBuilderFactory docBuilderfactory = DocumentBuilderFactory.newInstance();
        retDocument = docBuilderfactory.newDocumentBuilder().newDocument();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return retDocument;
}

From source file:Main.java

public static Document String2XML(String xmlStr) {
    try {// w  w w.j a  v a  2s . co m
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(is);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Document fromString(String text) {
    try {/*from   www .j  a  v  a2s.c  om*/

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(text);

        doc.getDocumentElement().normalize();

        return doc;

    } catch (IOException | ParserConfigurationException | SAXException e) {

    }

    return null;
}

From source file:Main.java

public static Document getDOMMaker() throws ParserConfigurationException {
    //get an instance of factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //get an instance of builder
    DocumentBuilder db = dbf.newDocumentBuilder();
    //create an instance of DOM
    Document dom = db.newDocument();
    return dom;/*from  w  w w  .ja  v a  2s  .c  o m*/
}

From source file:Main.java

/**
 * Returns a new DOM document for the given URI.
 * /*from  w  w w  .  j  a  v a 2s  .  c  o m*/
 * @param uri
 *            URI to parse into the document.
 * @return Returns a new DOM document for the given URI.
 */
public static Document loadDocument(String uri) {
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        return docBuilder.parse(uri);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Makes a DOM document out of an XML file.
 * @param pathToFile path to the XML file.
 * @return a DOM document//from w  w w  . j  a  v a 2  s.c  om
 */
public static Document parseXmlFile(String pathToFile) {
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(pathToFile);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (final ParserConfigurationException e) {
        throw new RuntimeException(e); // TODO proper exception handling
    } catch (final SAXException e) {
        throw new RuntimeException(e); // TODO proper exception handling
    } catch (final IOException e) {
        throw new RuntimeException(e); // TODO proper exception handling
    }
}

From source file:Main.java

public static Document getDocumentFromXML(String mappingFile, String schemaFile) {
    try {/*  w  w w.ja v a  2  s  . c  o m*/
        if (schemaFile != null) {
            if (validateXML(schemaFile, mappingFile) == false) {
                return null;
            }
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(mappingFile);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object getXMLValue(String xml, String xQuery, QName resultType)
        throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

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

    XPathFactory xPathfactory = XPathFactory.newInstance();

    XPath xPath = xPathfactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(xQuery);
    return xPathExpression.evaluate(doc, resultType);
}