Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的XML SAXParserException_Android_Xml_Xpath_Xml Parsing_Android Xml - Fatal编程技术网

android中的XML SAXParserException

android中的XML SAXParserException,android,xml,xpath,xml-parsing,android-xml,Android,Xml,Xpath,Xml Parsing,Android Xml,使用下面的代码,我试图获取xml中的节点列表,但应用程序崩溃,引发saxparser异常 这就是我正在尝试使用的xml <?xml version="1.0"?><Root><ResponseCode>1</ResponseCode><ResponseMessage>Reported Successfully</ResponseMessage><ResultSet><Post><id>1&

使用下面的代码,我试图获取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>
1我家附近的垃圾问题报道成功我们这些市民自2004年以来一直面临垃圾问题,请解决它,当局关注24.81868867.029686垃圾112013-03-23 14:44:432
这就是我试图从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());

    }
InputSource is=new InputSource();
is.setCharacterStream(新StringReader(结果));
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
字符串代码=xPath.evaluate(“/Root/ResponseCode/text()”,is);
如果(代码比较到(“1”)==0){
Log.d(“响应代码”,代码);
NodeList NodeList=(NodeList)xPath.evaluate(“//Post”,即XPathConstants.NODESET);
对于(inti=0;i
public类SAXXmlParser){
私有静态最终字符串ROOT\u ELEMENT=“ROOT”;
私有字符串ResponseCode=“ResponseCode”;
私有字符串ResponseMessage=“ResponseMessage”;
私有LinkedHashMap resultMap=null;
私有字符串xml=”“;
公共SAXXmlParser(字符串str){
this.xml=str;
resultMap=newlinkedhashmap();
};
受保护的InputStream getInputStream()
{
ByteArrayInputStream是=新的ByteArrayInputStream(xml.getBytes());
回报是;
}
公共LinkedHashMap解析()
{
RootElement root=新的RootElement(root\u元素);
setEndElementListener(新的EndElementListener()
{
公共无效结束()
{
}
});
root.getChild(ResponseCode.setEndTextElementListener(新的EndTextElementListener())
{
公共无效结束(字符串体)
{
结果图put(“响应代码”,正文);
}
});
root.getChild(ResponseMessage.setEndTextElementListener(新的EndTextElementListener())
{
公共无效结束(字符串体)
{
结果映射put(“响应消息”,正文);
}
});
尝试
{
parse(this.getInputStream(),Xml.Encoding.UTF_8,root.getContentHandler());
} 
捕获(例外e)
{
抛出新的运行时异常(e);
}
返回结果图;
}
}
使用这个示例类, 并称为

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"));
LinkedHashMap结果映射=新建LinkedHashMap();
SAXXmlParser=新解析器(xml);
resultMap=parser.parse();
Log.d(“ResponseCode”,resultMap.get(“ResponseCode”);
Log.d(“ResponseMessage”,resultMap.get(“ResponseMessage”);

问题是,一旦使用了InputStream,我们就不能再使用它了。在本例中,首先它被用来从xml获取响应代码,下一次它被用于post,这里出现异常,因为下一次我们调用它时,它没有什么可提供的,因此解析失败。通过创建另一个新的输入对象解决了问题然后使用它从xml获取帖子。

有什么例外?请上传日志,所以我删除了asnwer,因为这不符合我的想法,所以这是一个奇怪的问题。
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"));