2

In one of my android app when I am trying to parse an xml file from some url , sometimes i get ParseException: At line 1, column 0: no element found SAXException but the strange thing is I dont get this exception always means when I refresh the page again (going back to previous activity and coming again ) It starts working (means it parse the xml).

I could not find the exact reason why this is happening. It seems to me that It might be the problem of internet but the net is working on device and one more thing it does not happened always some time its prases the XML easily but sometimes it throws the exception.

Here is the code i am using for parsing :

xr.parse(new InputSource(new URL(urlToHit).openStream()));

I have tried another way also for parsing :

URL serverAdd=new URL(urlToHit);

     URLConnection connection = serverAdd.openConnection();

     xr.parse(new InputSource(connection.getInputStream()));

But the problem still continues.(Sometimes it parses the xml sometimes it cannot)

Please help as its very urgent for me .

Thanks.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60

3 Answers3

2

It has nothing to do with Android, perhaps the internet connection you have is not reliable hence the SAX parser is not getting a valid XML file

Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
  • Thanks for a quick reply. So what should I do to remove this problem as sometimes it works and sometimes it does not. I have used other methods for connection also but still the excetion comes some times. Please suggest – Dinesh Sharma Jan 04 '12 at 07:10
  • Try using DOM instead of SAX if the file is not too large or retry if you get an exception – Rajdeep Dua Jan 04 '12 at 07:12
  • I cant use DOM as my XML is large (for efficiency method) and I am retrying to parse XML(It works) but it reduces the efficiency of my app . I have to make my app live so please suggest if you have some alternate method .....? – Dinesh Sharma Jan 04 '12 at 07:26
0

I suggest not use URLConnection..Refer follwing snippet

It works without URLConnection..Try it..

XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL ("http://10.0.2.2:8080/folderName/question.xml");
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));`
Deval Shah
  • 1,094
  • 8
  • 22
0

First see this Question if it helps talking about the Question code and Answer both.

Okay now in that question I have set encoding type to the inputstream as is.setEncoding("ISO-8859-1"); try that in your case too.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98