Top Picks

   
February 27, 2013

 

Android XmlPullParser error

 

I’ve found good example how to parse XML in android.com. Here is part of

http://developer.android.com/training/basics/network-ops/xml.html

private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException { String link = ""; parser.require(XmlPullParser.START_TAG, ns, "link"); String tag = parser.getName(); String relType = parser.getAttributeValue(null, "rel"); if (tag.equals("link")) { if (relType.equals("alternate")) { link = parser.getAttributeValue(null, "href"); parser.nextTag(); } } parser.require(XmlPullParser.END_TAG, ns, "link"); return link; }

It can correctly parse when there is only one LINK tag in one single tag. But it output error when there is so many LINK in single tag.

Please let me know how to fix this coding to parse multiple LINK tags in one single tag.

 

Answer

I’m not sure why you are getting the error unless if you tell us what the error is but as far as checking the tags go, you need to fix it with a while loop:

parser.require(XmlPullParser.START_TAG, ns, "NameOfParentTag"); //Tag name which "link" tags are embedded in String link = ""; while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); String relType = parser.getAttributeValue(null, "rel"); if (tag.equals("link")) { if (relType.equals("alternate")) { link = parser.getAttributeValue(null, "href"); //Break here if you are done looking for what you need } } } return link;

It’d be helpful if you also provided an example XML that you want to parse.

Professional Hosting fro Just Host

Filed under: Android Coding FAQ

Tags:

Comments

No Comments

Leave a reply