我正在尝试使用SAX解析器http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml
解析Openweathermap API。但是当我设置xmlReader.parse(new InputSource(inputStream));
时,我收到了上述错误。
获取xml的代码 -
URL url = new URL(target_URL);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
if(HttpURLConnection.HTTP_OK == conn.getResponseCode()){
SAXParserCustom saxParserCustom = new SAXParserCustom();
saxParserCustom.parse(conn.getInputStream());
}
这就是我使用SAX解析XML文件的方法。
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
SAXHandler saxHandler = new SAXHandler();
xmlReader.setContentHandler(saxHandler);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s="";
while ((s=bufferedReader.readLine())!=null){
stringBuilder.append(s);
}
Log.d("SAX", stringBuilder.toString());
xmlReader.parse(new InputSource(inputStream));
上面的日志正确打印xml文件所以我的猜测是我在解析函数时出错了。