package com.robert.httphelper.converter; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.http.client.ClientProtocolException; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.robert.httphelper.converter.abs.AbstractStreamConverter; /** * <p> * Convert the http body to Document object. * </p> * * @author Robert Lee * @version 1.0 * @since Jul 15, 2015 */ public class Xml2DocumentConverter extends AbstractStreamConverter<Document> { @Override protected Document doConstructObject(InputStream is, Charset charset) throws IOException { DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); return docBuilder.parse(is, charset.toString()); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } }