0

我是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;
   }
 }
4

3 回答 3

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;
   }
 }
于 2012-06-27T13:03:19.607 回答
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;
   }
   }
于 2012-06-27T13:37:50.107 回答
-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;

    }
于 2012-06-27T13:10:14.360 回答