0

I am begginer to android development. I am using SAX parser for xml parser. I couldn't find the reason for this exeption. I tried getAsset() method. but it didn't worked.

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;
   }
 }
Roshni Kyada
  • 725
  • 1
  • 6
  • 10

3 Answers3

2

Why are you using a FileInputStream with a URL? Try:

 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;
   }
 }
nEx.Software
  • 6,782
  • 1
  • 26
  • 34
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;
   }
   }
-1

change your line

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

by

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

where callWebservice method is as shown below

 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;

    }
Khan
  • 7,585
  • 3
  • 27
  • 44
  • why use this new method while you have `URL("...").openStream()`? – ColdFire Jun 27 '12 at 13:14
  • @A.A in my case the url which i have used is not works with the above code and that's y i have used code as shown aboe which works almost for each and every url links – Khan Jun 27 '12 at 13:17