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

static public Document parseXml(String xmlText) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xmlText));
    return db.parse(is);
}

From source file:Main.java

public synchronized static Document loadDocument(File file)
        throws SAXException, IOException, ParserConfigurationException {
    if (file != null && file.exists()) {
        DocumentBuilder builder = createDocumentBuilder();
        if (builder != null) {
            return builder.parse(file);
        }/* w w  w. j  a  v a  2 s  .  c  om*/
    }
    return null;
}

From source file:Main.java

public synchronized static Document loadDocument(InputStream is)
        throws SAXException, IOException, ParserConfigurationException {
    if (is != null) {
        DocumentBuilder builder = createDocumentBuilder();
        if (builder != null) {
            return builder.parse(is);
        }//  w w w.  j av  a 2 s  .  c o m
    }
    return null;
}

From source file:Main.java

/**
 * getDocument w/InputStream/*from w ww  . j  a v a  2  s.c  om*/
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
public static Document getDocument(final InputStream inStream)
        throws ParserConfigurationException, SAXException, IOException {
    Document document;

    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

    dfactory.setNamespaceAware(true);

    DocumentBuilder dbuilder = dfactory.newDocumentBuilder();

    document = dbuilder.parse(inStream);

    return document;
}

From source file:Main.java

/**
 * Parses an input stream of xml data into a DOM tree
 * //from w w w. jav a  2 s.co m
 * @param is
 * @return the DOM tree
 */
public static Document parse(InputStream is) {
    try {
        Document document = null;

        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(is);

        return document;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}

From source file:Main.java

public static Document getAbsoluteXML(Node xml) {
    Document doc = null;//from   www. j  ava 2s. c o m
    String xmlContent = getString(xml);
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = db.parse(new InputSource(new ByteArrayInputStream(xmlContent.getBytes("utf-8"))));
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Main.java

public static Document parseXml(InputStream in) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilder builder = getBuilder();
    InputStream bin = new BufferedInputStream(in);
    Document ret = builder.parse(new InputSource(bin));
    return ret;/*from   ww  w .  j ava  2s  . com*/
}