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

/**
 * Parses XML document/*from   www.j  a  v a2 s .  c  o m*/
 * @param string XML document as string
 * @return XML document as object
 * @throws IOException if error reading document
 * @throws SAXException if provided string is not an XML
 * @throws ParserConfigurationException if unable to create parser
 */
public static Document toDocument(String string)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(string)));
}

From source file:Main.java

public static Document toDocument(InputStream input)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    return input == null ? null : db.parse(input);
}

From source file:Main.java

public static Document getXmlDocument(String path)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document document = builder.parse(new FileInputStream(path));

    return document;
}

From source file:Main.java

private static Document LoadXml(InputStream stream) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  w ww  .j a  v  a 2 s  .c  o m*/

    // builder...
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(stream);

    // return...
    return doc;
}

From source file:Main.java

/**
 * Loads an XML document from the given file.
 * @param file XML file to load.// w w  w  .  jav  a  2 s  .  co m
 * @param isValidating true: XML doc is validated against the DTD specifed
 *   within the XML document, false: otherwise.
 *   This does not work for XML Schemata! See {@link #load(File, Schema)}
 *   for that.
 * @param isNamespaceAware true: XML doc is namespace aware, false: otherwise.
 * @return Returns an XML document.
 * @throws IOException Throws an exception if the file couldn't be read.
 */
static public Document load(File file, boolean isValidating, boolean isNamespaceAware) throws IOException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(isValidating);
        factory.setNamespaceAware(isNamespaceAware);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return (builder.parse(file));
    } catch (ParserConfigurationException pce) {
        throw new IOException("Parser configuration error: " + pce);
    } catch (SAXException se) {
        throw new IOException("XML parsing error: " + se);
    }
}

From source file:Main.java

public static String getRequestID(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else/*  w ww  . j a  va  2 s .  co m*/
        return null;
}

From source file:Main.java

public static String getRequestName(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-name").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else//from  w w  w .  jav a2 s  .c  om
        return null;
}

From source file:Main.java

public static String getUnicastID(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("unicast-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else//from   ww w . jav a 2s  .co  m
        return null;
}

From source file:Main.java

public static boolean isValidXml(String xml) {
    try {// www  .  ja v  a  2  s . c  o  m
        StringReader reader = new StringReader(xml);
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        db.parse(new InputSource(reader));
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * @param is the input stream to read//  w w w.  j  a va 2  s. c o m
 * @return the document contained in that input stream
 */
public static Document readStream(InputStream is) {
    ensureFactory();
    try {
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        return builder.parse(is);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}