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 toDocument(String content)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    return content == null ? null : db.parse(new InputSource(new StringReader(content)));
}

From source file:Main.java

/**
 *
 * @param xml/*from   w  w  w .j a v a 2s  .c o  m*/
 * @return
 * @throws SAXException
 * @throws ParserConfigurationException
 * @throws IOException
 */
public static Document bytesToDom(byte[] xml) throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml));

}

From source file:Main.java

/**
 * @param xml is XML for a document//from   www.jav  a2 s.  c o m
 * @return the Document contained in that XML
 */
public static Document readString(String xml) {
    ensureFactory();
    try {
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/** Parse the provided {@link File} as XML, decompressing it first if it has a .gz file extension. */
public static Document parseXmlFile(File file) throws IOException {
    InputStream in = toInputStream(file);
    try {//from  ww  w. j a  va2s . c o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(in);
    } catch (Exception e) {
        throw new RuntimeException("failed to parse", e);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static Document getXMLDocumentFromString(String xml)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;/*from  ww  w .  j av a2  s  . c  o  m*/
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xml)));
    return doc;
}

From source file:Main.java

/**
 * Return the root node of a XML document.
 * @param fileNameXML name of the XML file
 * @throws ParserConfigurationException/*ww  w  .  ja va2 s .c o m*/
 * @throws SAXException
 * @throws IOException
 */
public static Document getXMLDocument(String fileNameXML)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(fileNameXML);
    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 getXMLDocumentFromScratch(String rootElementName)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;//from  w w w.  j  a va  2  s  .  co m
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader("<" + rootElementName + "></" + rootElementName + ">")));
    return doc;
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);//from  w w  w .ja v  a 2s  .  co m

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

/**
 * //  w  ww . j a  va 2 s .  c  o m
 * <B>Purpose:</B> Parses the String to an XML Document Object
 * 
 * @param xml
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xml)));

}

From source file:Main.java

/**
 * Parse an XML document.//from ww  w  . j  a  v a2  s.  c  om
 * 
 * @param in xml input stream
 * @return document
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parseXml(InputStream in) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.parse(in);
}