com.jianggujin.http.response.JXMLSaxResponse Maven / Gradle / Ivy
package com.jianggujin.http.response;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import com.jianggujin.http.core.JParseException;
import com.jianggujin.http.util.JDataUtils;
/**
* XML响应,SAX方式解析
*
* @author jianggujin
*
*/
public class JXMLSaxResponse extends JAbstractResponse {
private String charset;
private JSaxDefaultHandler handler;
public JXMLSaxResponse(JSaxDefaultHandler handler) {
this.handler = handler;
}
public JXMLSaxResponse(String charset, JSaxDefaultHandler handler) {
this(handler);
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;
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
InputStreamReader reader = new InputStreamReader(stream, charset);
parser.parse(new InputSource(reader), handler);
this.data = handler.getData();
} catch (Exception e) {
throw new JParseException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy