使用下面的代码,我试图获取 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());
}
这是日志猫:
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