All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jianggujin.http.response.JXMLDomResponse Maven / Gradle / Ivy

package com.jianggujin.http.response;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.jianggujin.http.core.JParseException;
import com.jianggujin.http.util.JDataUtils;

/**
 * XML响应,DOM方式解析
 * 
 * @author jianggujin
 *
 */
public class JXMLDomResponse extends JAbstractResponse {
   private String charset;

   public JXMLDomResponse() {
   }

   public JXMLDomResponse(String charset) {
      this.charset = charset;
   }

   @Override
   protected void dealData(InputStream stream) throws IOException {
      String charset = (this.charset == null || this.charset.length() == 0)
            ? JDataUtils.getCharsetFromContentType(this.getHeader("Content-Type"))
            : this.charset;
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      try {
         InputStreamReader reader = new InputStreamReader(stream, charset);
         DocumentBuilder db = dbf.newDocumentBuilder();
         this.data = db.parse(new InputSource(reader));
      } catch (Exception e) {
         throw new JParseException(e);
      }
   }

   public Document getDocument() {
      return (Document) this.data;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy