Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 SaxParser XMLReader.parse()和InputSource参数_Android_Xml_Parsing_Saxparser - Fatal编程技术网

Android SaxParser XMLReader.parse()和InputSource参数

Android SaxParser XMLReader.parse()和InputSource参数,android,xml,parsing,saxparser,Android,Xml,Parsing,Saxparser,我正在尝试使用SaxParser解析xml文件资源。我已经创建了我的DataHandler,但我不知道如何向XmlReader指示data.xml在res/xml/中的位置 InputSource对象的正确参数是什么 XmlResourceParser parser = getResources().getXml(R.xml.data); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXPa

我正在尝试使用SaxParser解析xml文件资源。我已经创建了我的DataHandler,但我不知道如何向XmlReader指示data.xml在res/xml/中的位置

InputSource对象的正确参数是什么

    XmlResourceParser parser = getResources().getXml(R.xml.data);       
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();

    // Create handler to handle XML Tags ( extends DefaultHandler ) 
    DataSaxHandler myXMLHandler = new DataSaxHandler();
    xr.setContentHandler(myXMLHandler);
    //R.xml.data is my xml file
InputSource is=new InputSource(getResources().getXml(R.xml.data));  //getResources... is wrong say Eclipse

    xr.parse(is);       

非常感谢。

问题在于对getResources().getXml(int-id)的调用返回了一个XmlResourceParser,并且没有接受XmlResourceParser的InputSource构造函数

如果您想继续使用SaxParser,则需要通过Resources#openRawResource(intid)打开一个InputStream,然后将其传递给InputSource构造函数。您还需要将文件移动到res/raw才能使用openrawsource函数。

抱歉。。。。。