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;
   }
 }
2
  • 1
    new FileInputStream("http://64.85.165.53/dharatest/xmlarray.xml") ??? Commented Jun 27, 2012 at 12:59
  • 1
    you need to download your .xml file from that url first and then u can read/parse that XML. Commented Jun 27, 2012 at 13:02

3 Answers 3

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;
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

    }

2 Comments

why use this new method while you have URL("...").openStream()?
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.