Example usage for javax.xml.parsers DocumentBuilder parse

List of usage examples for javax.xml.parsers DocumentBuilder parse

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder parse.

Prototype


public abstract Document parse(InputSource is) throws SAXException, IOException;

Source Link

Document

Parse the content of the given input source as an XML document and return a new DOM Document object.

Usage

From source file:Main.java

private static Document readString(String xmlSource)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    return docBuilder.parse(new ByteArrayInputStream(xmlSource.getBytes(StandardCharsets.UTF_8)));
}

From source file:Main.java

/**
 * XML DOM helper.//w ww  .  j ava  2 s  . c  o m
 * 
 * @param is
 * @return the XML DOM document object
 * @throws Exception
 */
public static Document loadDocument(InputStream is) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(is);
    return doc;
}

From source file:Main.java

/**
 * Loads the given file using the default XML DOM implementation.
 * //w  w w . j av a2s  . c o  m
 * @param documentFile The file to load.
 * @return The DOM document contained in the file.
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document loadDocument(File documentFile)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder documentBuilder = getDocumentBuilder();

    return documentBuilder.parse(documentFile);
}

From source file:Main.java

public static Document parseXml(InputStream input) {
    try {//from   w w  w  .j a v  a2 s  .c o m
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        fac.setValidating(false);

        DocumentBuilder docBuilder = fac.newDocumentBuilder();
        Document doc = docBuilder.parse(input);

        return doc;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

public static Document fromString(String text) {
    try {// w  w  w. ja v a 2s .  co  m

        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 loadXml(File sourceFile) {
    try {/*w ww.ja  v a  2 s  . c om*/
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document baseSizeDoc = dBuilder.parse(sourceFile);

        baseSizeDoc.getDocumentElement().normalize();

        return baseSizeDoc;
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }

    throw new IllegalStateException("Could not load xml file: " + sourceFile.getPath());
}

From source file:Main.java

/**
 * @param is/*w w w. j  a  v a  2 s .co m*/
 * @return
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
public static Document parseInputStream(InputStream is) throws IOException {
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);
        domFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        return builder.parse(is);
    } catch (Exception e) {
        throw new IOException("Error parsing XML Stream", e);
    }
}

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/* w w  w.  j  a  v  a 2  s .c  o m*/
 */
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 readXML(File file) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true); // never forget this!
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    return documentBuilder.parse(file);
}

From source file:Main.java

public static Document parse(String text) throws IOException, SAXException {
    final DocumentBuilder documentBuilder = DOCUMENT_BUILDER.get();
    final Document document = documentBuilder.parse(new InputSource(new StringReader(text)));
    documentBuilder.reset();//w w w.  j av  a2  s  .  c  o m
    return document;
}