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 Document parse(InputStream is, String systemId) throws SAXException, IOException 

Source Link

Document

Parse the content of the given InputStream as an XML document and return a new DOM Document object.

Usage

From source file:interoperabilite.webservice.fluent.FluentResponseHandling.java

public static void main(String[] args) throws Exception {
    Document result = Request.Get("http://somehost/content").execute()
            .handleResponse(new ResponseHandler<Document>() {

                @Override/*w w w . ja  v  a 2 s  .  c o  m*/
                public Document handleResponse(final HttpResponse response) throws IOException {
                    StatusLine statusLine = response.getStatusLine();
                    HttpEntity entity = response.getEntity();
                    if (statusLine.getStatusCode() >= 300) {
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        ContentType contentType = ContentType.getOrDefault(entity);
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = Consts.ISO_8859_1;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                }

            });
    // Do something useful with the result
    System.out.println(result);
}

From source file:io.aos.protocol.http.httpcommon.FluentResponseHandling.java

public static void main(String... args) throws Exception {
    Document result = Request.Get("http://somehost/content").execute()
            .handleResponse(new ResponseHandler<Document>() {

                public Document handleResponse(final HttpResponse response) throws IOException {
                    StatusLine statusLine = response.getStatusLine();
                    HttpEntity entity = response.getEntity();
                    if (statusLine.getStatusCode() >= 300) {
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }//from  ww  w  . j  a  va2s. c o m
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        ContentType contentType = ContentType.getOrDefault(entity);
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = HTTP.DEF_CONTENT_CHARSET;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                }

            });
    // Do something useful with the result
    System.out.println(result);
}