【问题标题】:FileNotFound Exception while passing inputStream to XmlParser将 inputStream 传递给 XmlParser 时出现 FileNotFound 异常
【发布时间】:2012-06-28 21:58:25
【问题描述】:

我是 android 开发的初学者。我将 SAX 解析器用于 xml 解析器。我找不到这个例外的原因。 我尝试了 getAsset() 方法。但它没有用。

xmlParser 代码:::

 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;
   }
 }

【问题讨论】:

  • new FileInputStream("http://64.85.165.53/dharatest/xmlarray.xml") ???
  • 您需要先从该网址下载您的 .xml 文件,然后您才能读取/解析该 XML。

标签: android inputstream saxparser filenotfoundexception


【解决方案1】:

为什么要使用带有 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;
   }
 }

【讨论】:

    【解决方案2】:

    换行

    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;
    
        }
    

    【讨论】:

    • 你有URL("...").openStream()时为什么要使用这种新方法?
    • @A.A 在我的情况下,我使用的 url 不适用于上面的代码,那就是我使用了上面所示的代码,它几乎适用于每个 url 链接
    【解决方案3】:
     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;
       }
       }
    

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      相关资源
      最近更新 更多