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

public static Document load(String xml) throws Exception {
    DocumentBuilder builder = getDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document;
}

From source file:Main.java

public static Document stringToDocument(final String string)
        throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from  w w w  .  j  ava2  s  . c  om
    final DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes(ENCODING))));
}

From source file:Main.java

private static Document initializeXML(File xmlFile) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    doc.getDocumentElement().normalize();
    return doc;/*from ww  w .  j ava2 s  . c om*/
}

From source file:Main.java

private static Document loadXmlFromInputSource(InputSource is) {
    try {/*from  w ww .j  ava  2  s .  co  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.parse(is);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document loadXml(String filename) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(filename);
    return document;
}

From source file:Main.java

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

From source file:Main.java

public static String getXmlContent() throws ParserConfigurationException, SAXException, IOException {
    File file = new File("src/main/resources/tesco/data/input/priceChange.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(file);

    return doc.toString();
}

From source file:Main.java

public static Document getDocument() throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(filename);
    return document;
}

From source file:Main.java

/**
 * Returns Document object of the xml located at the given path
 * /*from ww w .j  ava2 s  .c  om*/
 * @param path path to xml
 * @return Document object
 * @throws ParserConfigurationException If parsing error occurs
 * @throws SAXException If parsing error occurs
 * @throws IOException If IO error occurs.
 */
public static Document getXMLDocument(String path)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(path);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

public static Document getDocument(String xml) {
    if (xml == null || xml.indexOf("<?xml") < 0)
        return null;
    try {//w  w w  .j a v a  2  s  .  com
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}