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 Element unmarshalDOMElement(byte[] input)
        throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new ByteArrayInputStream(input));

    return doc.getDocumentElement();
}

From source file:Main.java

public static Document parse(InputStream fStream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    return builder.parse(fStream);
}

From source file:Main.java

public static Document getDocument(String xmlDocument) {
    try {/*from  w  ww .  j  a  va 2s  .  com*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xmlDocument.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new RuntimeException("Error parsing XML document", e);
    }
}

From source file:Main.java

public static Document stringToDOM(String xmlString) {
    try {//  ww w  .  ja v a  2s . c om
        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

public static Document fileToDOM(File file) {
    try {//from   w w  w. j a  v a2  s .  co  m
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        return docBuilder.parse(file);
    } catch (Throwable t) {
        return null;
    }
}

From source file:Main.java

public static Document readXMLFile(String filename) throws IOException {

    DocumentBuilder builder;/*from   w ww .  ja  v  a  2  s  .c om*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        builder = factory.newDocumentBuilder();
        Document document = builder.parse(filename);
        return document;
    } catch (Exception e) {
        throw new IOException();
    }
}

From source file:Main.java

public static Element buildDoc(String content) throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse(new ByteArrayInputStream(content.getBytes("UTF-8")));
    Element docEle = dom.getDocumentElement();

    return docEle;
}

From source file:Main.java

public static Document loadFromFile(File file)
        throws IllegalAccessException, ParserConfigurationException, SAXException, IOException {
    if (!file.exists()) {
        throw new IllegalAccessException("File is not exists: " + file.getAbsolutePath());
    }//from  w ww. jav  a 2 s  . c  o m

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(file);
}

From source file:Main.java

public static Element parseElement(String xml) {
    try {/*from  w  w  w .  ja  v  a2s. c om*/
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc.getDocumentElement();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Convenience function to get a DOM document builder object.
 * // w  ww  . j  av  a  2s .  c  o  m
 * @return A new <code>DocumentBuilder</code> instance.
 * @throws ParserConfigurationException
 */
public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

    return documentBuilderFactory.newDocumentBuilder();
}