开发者

Android XML parsing

A strange problem has cropped up... I checked several websites but couldn't find anything wrong with the code... But it returns NullPointerException...

try { 

    SAXParserFactory f = SAXParserFactory.newInstance();
    SAXParser parser = f.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setContentHandler(handler);
    System.out.println(uri_this);
    reader.parse(new InputSourc开发者_JAVA百科e(url_this.openStream())); // i get nullpointerexception here 

} catch (NullPointerException e) {
    System.out.println(e);
}

In the catch statement I have caught NullPointerException and made it print the error.

The ContentHandler is fine because I have got parsed files for small files but for larger files, it doesn't seem to work. What is the problem? Or do I need some buffering for larger files? I'm clueless and have tried all possibilities except buffering because I don't know the coding for that.

I figured out 1 thing: the object of the input source is freed before the reader.parse can finish. I don't understand why it's happening.

The LogCat gives the following (only the erroneous part):

01-06 15:12:27.145: INFO/System.out(319): http://mywebsite.com/products_xml.php?cPath=43
01-06 15:12:31.094: DEBUG/dalvikvm(319): GC freed 4031 objects / 203424 bytes in 189ms
01-06 15:12:31.505: DEBUG/dalvikvm(100): GC freed 3326 objects / 190192 bytes in 283ms
01-06 15:12:34.274: DEBUG/dalvikvm(319): GC freed 1153 objects / 80432 bytes in 146ms
01-06 15:12:34.646: INFO/System.out(319): java.lang.NullPointerException

I'm getting the url "url_this" 4m the constructor as arguments.


There not enough info to help you here but did you notice that you print out a variable called uri_this in the shell but dereference url to be parsed? So just because the logfile shows http://mywebsite.com/products_xml.php?cPath=43 it does not automatically mean, that the variable url is initialized and also contains that string.

Or it could be that the openStream() method on the url object (whatever type it is) returns null in some conditions and InputSource can not handle being passed null in the constructor. As I said - there is not enough info in the short snipplet to solve your problem.

Cheers.


Hey thanx all of u. I got my mistake. actually in 1 case there was no text between start and end tag... hence the variable i stored text in was null... i called .toString() on that. resulting in an exception - NullPointerException. Thanx all 4 ur help.


I just released a library to make XML parsing on Android 10x easier than it currently is. It is a very small abstraction layer that sits on top of the existing xml pull parsing libraries provided by the runtime and allow you to use simple xpath-like paths to elements to get parsed values back.

Performance impact is almost nothing on top of the existing parse operation, it is a very simple/clean implementation: http://www.thebuzzmedia.com/software/simple-java-xml-parser-sjxp/


check the above Url http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

changes made in XMLParser.java

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

it will works InshaAllah...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜