将InputStream传递给XmlParser时出现FileNotFound异常

时间:2012-06-27 12:56:44

标签: android inputstream saxparser filenotfoundexception

我对android开发很感兴趣。我正在为xml解析器使用SAX解析器。我找不到这个例子的原因。 我尝试了getAsset()方法。但它没有用。

xmlParser code :::

 public class XMLParser {

     public static Country parseCountry(InputStream is) {
        try {
         Country country= new Country(null, null, null);

         XMLReader xmlReader =  SAXParserFactory.newInstance().newSAXParser().getXMLReader();
         XMLHandler xmlHandler = new XMLHandler();
         xmlReader.setContentHandler(xmlHandler);     
         xmlReader.parse(new InputSource(new FileInputStream(http://64.85.165.53/dharatest/xmlarray.xml));
         country = xmlHandler.getParsedCountryData();

        } catch(ParserConfigurationException pce) { 
               Log.e("SAX XML", "sax parse error", pce); 
        } catch(SAXException se) { 
               Log.e("SAX XML", "sax error", se);       
        } catch(IOException ioe) { 
               Log.e("SAX XML", "sax parse io error", ioe); 
        }     
       return country;
   }
 }

3 个答案:

答案 0 :(得分:2)

为什么使用带有URL的FileInputStream?尝试:

 public class XMLParser {

     public static Country parseCountry(InputStream is) {
        try {
         Country country= new Country(null, null, null);

         XMLReader xmlReader =  SAXParserFactory.newInstance().newSAXParser().getXMLReader();
         XMLHandler xmlHandler = new XMLHandler();
         xmlReader.setContentHandler(xmlHandler);     
         xmlReader.parse(new InputSource(new URL("http://64.85.165.53/dharatest/xmlarray.xml").openStream());
         country = xmlHandler.getParsedCountryData();

        } catch(ParserConfigurationException pce) { 
               Log.e("SAX XML", "sax parse error", pce); 
        } catch(SAXException se) { 
               Log.e("SAX XML", "sax error", se);       
        } catch(IOException ioe) { 
               Log.e("SAX XML", "sax parse io error", ioe); 
        }     
       return country;
   }
 }

答案 1 :(得分:0)

 public class XMLParser {

     public static Country parseCountry(InputStream is) {
         try {
         Country country= new Country(null, null, null);

       XMLReader xmlReader =SAXParserFactory.newInstance().newSAXParser().getXMLReader();
     XMLHandler xmlHandler = new XMLHandler();
     xmlReader.setContentHandler(xmlHandler);     
     xmlReader.parse(new InputSource(getAssets().open("data.xml"));
     country = xmlHandler.getParsedCountryData();

    } catch(ParserConfigurationException pce) { 
           Log.e("SAX XML", "sax parse error", pce); 
    } catch(SAXException se) { 
           Log.e("SAX XML", "sax error", se);       
    } catch(IOException ioe) { 
           Log.e("SAX XML", "sax parse io error", ioe); 
    }     
  return country;
   }
   }

答案 2 :(得分:-1)

更改行

xmlReader.parse(new InputSource(new FileInputStream(http://64.85.165.53/dharatest/xmlarray.xml));  

通过

 xmlReader.parse(new InputSource(callWebservice("http://64.85.165.53/dharatest/xmlarray.xml")));

其中 callWebservice 方法如下所示

 private InputStream callWebservice(String serviceURL) {
        HttpClient client=new DefaultHttpClient();
        HttpGet getRequest=new HttpGet();

           try {
             // construct a URI object
             getRequest.setURI(new URI(serviceURL));
              } catch (URISyntaxException e) {
                Log.e("URISyntaxException", e.toString());
               }

            // buffer reader to read the response
           BufferedReader in=null;
           // the service response
          HttpResponse response=null;
             try {
                  // execute the request
                  response = client.execute(getRequest);
               } catch (ClientProtocolException e) {
                 Log.e("ClientProtocolException", e.toString());
               } catch (IOException e) {
                 Log.e("IO exception", e.toString());
            }
      if(response!=null)
            try {
                return response.getEntity().getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        else
          return null;
        return null;

    }