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 getXMLDOM(String currentFile)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(currentFile);
}

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 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 Element parseElement(String xml) {
    try {/*w w  w. ja v  a  2 s  . com*/
        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

public static synchronized Document inputStream2XML(InputStream streamedXml)
        throws SAXException, IOException, ParserConfigurationException {
    synchronized (domBuilderFactory) {
        DocumentBuilder builder = domBuilderFactory.newDocumentBuilder();
        return builder.parse(streamedXml);
    }//from w  w w .  j  ava  2 s . co  m

}

From source file:Main.java

public static Document load(String filename) {
    Document document = null;/*from w  ww.  j  av  a  2  s  . c  om*/
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new File(filename));
        document.normalize();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return document;
}

From source file:Main.java

public static Document getXMLDOM(URL url) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();

    return db.parse(url.openStream());
}

From source file:Main.java

/**
 * Converts a String representing an XML snippet into an {@link org.w3c.dom.Element}.
 *
 * @param xml snippet as a string/*from   ww w. j  a  v  a2s  .  c o  m*/
 * @return a DOM Element
 * @throws Exception if unable to parse the String or if it doesn't contain valid XML.
 */
public static Element stringToElement(String xml) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("utf8"));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document d = builder.parse(bais);
    bais.close();
    return d.getDocumentElement();
}

From source file:Main.java

public static Document getStringToDocument(String xml)
        throws TransformerException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbFactory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document;
}

From source file:Main.java

/**
 * @param path//  w w w  . jav  a  2  s  .c  o m
 * @return
 * @throws FileNotFoundException
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document parseXMLDom(String path)
        throws FileNotFoundException, SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    return builder.parse(new FileInputStream(path));
}