【问题标题】:XML SAXParserException in androidandroid中的XML SAXParserException
【发布时间】:2013-03-24 07:53:28
【问题描述】:

使用以下 I 代码,我试图获取 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

【问题讨论】:

  • 有什么异常?请上传logcat
  • 所以我删除了 asnwer 因为我的想法不起作用所以这是一个奇怪的问题。

标签: android xml xpath xml-parsing android-xml


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    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"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2012-04-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      相关资源
      最近更新 更多