使用以下我的代码我试图获取xml中的节点列表,但应用程序崩溃抛出saxparser异常。
这是我尝试使用的xml
<?xml version="1.0"?><Root><ResponseCode>1</ResponseCode><ResponseMessage>Reported Successfully</ResponseMessage><ResultSet><Post><id>1</id><title>Garbage Problem near my home</title><comment>We the citizens have been facing the problems of garbage since 2004 , kindly fix it , authorities are concerned</comment><imagepath></imagepath><cordx>24.818688</cordx><cordy>67.029686</cordy><tag>Garbage</tag><userid>1</userid><departmentid>1</departmentid><response></response><createdOn>2013-03-23 14:44:43</createdOn><status>2</status></Post></ResultSet></Root>
这是我试图从xml中获取的代码
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(result));
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String code = xPath.evaluate("/Root/ResponseCode/text()", is);
if(code.compareTo("1")==0){
Log.d("Responce Code", code);
NodeList nodeList = (NodeList) xPath.evaluate("//Post", is,XPathConstants.NODESET);
for(int i=0; i<nodeList.getLength(); i++){
Node n=nodeList.item(i);
Element element = (Element) n;
Log.d("LIST DATA", element.getElementsByTagName("id").item(0).getTextContent());
}
这是logcat:
03-24 12:31:16.357: I/Post service Response(901): <?xml version="1.0"?><Root><ResponseCode>1</ResponseCode><ResponseMessage>Reported Successfully</ResponseMessage><ResultSet><Post><id>1</id><title>Garbage Problem near my home</title><comment>We the citizens have been facing the problems of garbage since 2004 , kindly fix it , authorities are concerned</comment><imagepath></imagepath><cordx>24.818688</cordx><cordy>67.029686</cordy><tag>Garbage</tag><userid>1</userid><departmentid>1</departmentid><response></response><createdOn>2013-03-23 14:44:43</createdOn><status>2</status></Post></ResultSet></Root>
03-24 12:31:16.498: D/Responce Code(901): 1
03-24 12:31:16.537: W/System.err(901): org.xml.sax.SAXParseException: Unexpected end of document
03-24 12:31:16.537: W/System.err(901): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:129)
03-24 12:31:16.537: W/System.err(901): at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:474)
03-24 12:31:16.548: W/System.err(901): at com.teamgreen.greenit.HistoryActivity$1.run(HistoryActivity.java:69)
03-24 12:31:16.548: W/System.err(901): --------------- linked to ------------------
03-24 12:31:16.548: W/System.err(901): javax.xml.xpath.XPathExpressionException: org.xml.sax.SAXParseException: Unexpected end of document
03-24 12:31:16.577: W/System.err(901): at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:479)
03-24 12:31:16.577: W/System.err(901): at com.teamgreen.greenit.HistoryActivity$1.run(HistoryActivity.java:69)
03-24 12:31:16.587: W/System.err(901): Caused by: org.xml.sax.SAXParseException: Unexpected end of document
03-24 12:31:16.587: W/System.err(901): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:129)
03-24 12:31:16.587: W/System.err(901): at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:474)
03-24 12:31:16.607: W/System.err(901): ... 1 more
答案 0 :(得分:4)
问题是,一旦消耗了InputStream,在这种情况下我们不能再次使用它首先它用于从xml获取响应代码,下次用于发布时,这里发生异常,因为下次我们叫它,它没什么可给的,所以解析失败了。通过创建InputStream的另一个新对象然后使用它来从xml获取帖子来解决问题。
答案 1 :(得分:0)
public class SAXXmlParser {
private static final String ROOT_ELEMENT="Root";
private String ResponseCode="ResponseCode";
private String ResponseMessage="ResponseMessage";
private LinkedHashMap<String,String> resultMap=null;
private String xml="";
public SAXXmlParser(String str) {
this.xml=str;
resultMap=new LinkedHashMap<String, String>();
};
protected InputStream getInputStream()
{
ByteArrayInputStream is=new ByteArrayInputStream(xml.getBytes());
return is;
}
public LinkedHashMap<String,String> parse()
{
RootElement root = new RootElement(ROOT_ELEMENT);
root.setEndElementListener(new EndElementListener()
{
public void end()
{
}
});
root.getChild(ResponseCode).setEndTextElementListener(new EndTextElementListener()
{
public void end(String body)
{
resultMap.put("ResponseCode",body);
}
});
root.getChild(ResponseMessage).setEndTextElementListener(new EndTextElementListener()
{
public void end(String body)
{
resultMap.put("ResponseMessage",body);
}
});
try
{
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return resultMap;
}
}
使用此示例类, 并打电话给,
LinkedHashMap<String,String> resultMap =new LinkedHashMap<String,String>();
SAXXmlParser parser=new parser(xml);
resultMap =parser.parse();
Log.d("ResponseCode",resultMap.get("ResponseCode"));
Log.d("ResponseMessage",resultMap.get("ResponseMessage"));