2

使用下面的代码,我试图获取 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
4

2 回答 2

5

问题是,一旦 InputStream 被消费了,我们就不能再次使用它了它没有什么可提供的,因此解析失败。通过创建 InputStream 的另一个新对象然后使用它从 xml 获取帖子来解决该问题。

于 2013-03-25T21:21:21.183 回答
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"));
于 2013-03-25T04:31:43.153 回答