android - XmlPullParser - get data from tag with specific attributevalue -


i'm creating parser xml-file , works fine until add option retrieve link. have several tags of same name , want 1 specific tag specific attribute value.

... <artist>     <name>venom</name>     <mbid>5ddddef1-fd5a-4ca8-8e89-df4adff4239b</mbid>     <url>venom</url>     <image size="small">http://userserve-ak.last.fm/serve/34/35628151.png</image>     <image size="medium">http://userserve-ak.last.fm/serve/64/35628151.png</image>     <image size="large">http://userserve-ak.last.fm/serve/126/35628151.png</image>     <image size="extralarge">http://userserve-ak.last.fm/serve/252/35628151.png</image> </artist> ... 

i'm fishing link in "extralarge".

my current code:

... private trackinfo readtrack(xmlpullparser parser) throws xmlpullparserexception, ioexception {     parser.require(xmlpullparser.start_tag, ns, "track");     string artist = null;     string artistpic = null;     string artismbid = null;     string song = null;     string album = null;     while (parser.next() != xmlpullparser.end_tag) {         if (parser.geteventtype() != xmlpullparser.start_tag) {             continue;         }         string name = parser.getname();         if (name.equals("artist")) {              parser.require(xmlpullparser.start_tag, ns, "artist");             while (parser.next() != xmlpullparser.end_tag) {                 if (parser.geteventtype() != xmlpullparser.start_tag) {                     continue;                 }                 string artistname = parser.getname();                 if (artistname.equals("name")) {                     artist = readartist(parser);                 } else if (artistname.equals("mbid")) {                     artismbid = readartistmbid(parser);                 } else if (artistname.equals("image")) {                     artistpic = readartistpic(parser);                 } else {                     skip(parser);                 }             }         } else if (name.equals("name")) {             song = readsong(parser);         } else if (name.equals("album")) {             album = readalbum(parser);         } else {             skip(parser);         }     }     return new trackinfo(artist, artistpic, artismbid, song, album); }  ...  private string readartistpic(xmlpullparser parser) throws xmlpullparserexception, ioexception {     string artistpic = "";     parser.require(xmlpullparser.start_tag, ns, "image");     string tag = parser.getname();     string reltype = parser.getattributevalue(null, "size");     if (tag.equals("image")) {         if (reltype.equals("extralarge")) {             artistpic = readtext(parser);             parser.nexttag();         }     }     parser.require(xmlpullparser.end_tag, ns, "image");      return artistpic; }  ...  private string readtext(xmlpullparser parser) throws xmlpullparserexception, ioexception {     string result = "";     if (parser.next() == xmlpullparser.text) {         result = parser.gettext();         parser.nexttag();     }     return result; } ... 

the error:

e/currenttrackfragment logging﹕ xmlpullparserexception: org.xmlpull.v1.xmlpullparserexception: expected: end_tag {null}image (position:start_tag <image size='small'>@10:29 in java.io.inputstreamreader@52a3a014) 

anybody idea how handle this?

solution!

i started working example raghunandan gave me. in end simplified code , added 'check = true' suggested.

private trackinfo readtrack(xmlpullparser parser) throws xmlpullparserexception, ioexception {     parser.require(xmlpullparser.start_tag, ns, "track");     boolean nowplaying = false;     string artist = null;     string artistpic = null;     string artistmbid = null;     string song = null;     string songurl = null;     string album = null;     string albumpic = null;     int playeduts = 0;     while (parser.next() != xmlpullparser.end_tag) {         if (parser.geteventtype() != xmlpullparser.start_tag) {             continue;         }         string name = parser.getname();         if (name.equals("artist")) {             parser.require(xmlpullparser.start_tag, ns, "artist");             while (parser.next() != xmlpullparser.end_tag) {                 if (parser.geteventtype() != xmlpullparser.start_tag) {                     continue;                 }                 string artistname = parser.getname();                 if (artistname.equals("name")) {                     artist = readartist(parser);                 } else if (artistname.equals("mbid")) {                     artistmbid = readartistmbid(parser);                 } else if (artistname.equals("image")) {                     boolean check = false;                     string imagesize = parser.getattributevalue(null, "size");                     if (imagesize.equals("extralarge")) {                         check = true;                     }                     if (check) {                         artistpic = readartistpic(parser);                     } else {                         parser.nexttext();                     }                 } else {                     skip(parser);                 }             }         } else if (name.equals("name")) {             song = readsong(parser);         } else if (name.equals("url")) {             songurl = readsongurl(parser);         } else if (name.equals("album")) {             album = readalbum(parser);         } else if (name.equals("date")) {             playeduts = integer.parseint(readuts(parser));         } else {             skip(parser);         }     }     return new trackinfo(nowplaying, artist, artistpic, artistmbid, song, songurl,             album, albumpic, playeduts); }  ...  private string readartistpic(xmlpullparser parser) throws xmlpullparserexception, ioexception {     parser.require(xmlpullparser.start_tag, ns, "image");     string artistpic = readtext(parser);     parser.require(xmlpullparser.end_tag, ns, "image");     return artistpic; }  ... 

without full stacktrace hard point mistake. below works.

 public class xmlpullparserhandler {      private string text;      public xmlpullparserhandler() {      }     public void parse(inputstream is) { // pas input stream         xmlpullparserfactory factory = null;         xmlpullparser parser = null;         try {             factory = xmlpullparserfactory.newinstance();             factory.setnamespaceaware(true);             parser = factory.newpullparser();              parser.setinput(is, null);             boolean check =false;             //factory instantiates object              int eventtype = parser.geteventtype();             while (eventtype != xmlpullparser.end_document) {                 string tagname = parser.getname();                 switch (eventtype) {                 case xmlpullparser.start_tag:                     if (tagname.equalsignorecase("image")) {                          if(parser.getattributevalue(null, "size").equals("extralarge"))                         {                             check=true;                         }                      }                       break;                  case xmlpullparser.text:                     text = parser.gettext();                     break;                  case xmlpullparser.end_tag:                           string val = null;                           if  (tagname.equalsignorecase("name")) {                               val=text;                              log.i(""," name "+val);                           }                           else if (tagname.equalsignorecase("mbid"))                           {                               val=text;                              log.i(""," mbid "+val);                            }                           else if (tagname.equalsignorecase("url"))                           {                               val=text;                              log.i(""," url "+val);                            }                           else if (tagname.equalsignorecase("image"))                           {                               val=text;                              if(check == true)                              log.i(""," image "+val);                            }                       break;                  default:                     break;                 }                 eventtype = parser.next();             }          } catch (xmlpullparserexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          return null;     } } 

the log

04-24 04:39:06.906: i/(1539):  name venom 04-24 04:39:06.906: i/(1539):  mbid 5ddddef1-fd5a-4ca8-8e89-df4adff4239b 04-24 04:39:06.916: i/(1539):  url venom 04-24 04:39:06.916: i/(1539):  name http://userserve-ak.last.fm/serve/252/35628151.png 

alternative solution :

private trackinfo readtrack(xmlpullparser parser) throws xmlpullparserexception, ioexception {     parser.require(xmlpullparser.start_tag, ns, "track");     boolean nowplaying = false;     string artist = null;     string artistpic = null;     string artistmbid = null;     string song = null;     string songurl = null;     string album = null;     string albumpic = null;     int playeduts = 0;     while (parser.next() != xmlpullparser.end_tag) {         if (parser.geteventtype() != xmlpullparser.start_tag) {             continue;         }         string name = parser.getname();         if (name.equals("artist")) {             parser.require(xmlpullparser.start_tag, ns, "artist");             while (parser.next() != xmlpullparser.end_tag) {                 if (parser.geteventtype() != xmlpullparser.start_tag) {                     continue;                 }                 string artistname = parser.getname();                 if (artistname.equals("name")) {                     artist = readartist(parser);                 } else if (artistname.equals("mbid")) {                     artistmbid = readartistmbid(parser);                 } else if (artistname.equals("image")) {                     boolean check = false;                     string imagesize = parser.getattributevalue(null, "size");                     if (imagesize.equals("extralarge")) {                         check = true;                     }                     if (check) {                         artistpic = readartistpic(parser);                     } else {                         parser.nexttext();                     }                 } else {                     skip(parser);                 }             }         } else if (name.equals("name")) {             song = readsong(parser);         } else if (name.equals("url")) {             songurl = readsongurl(parser);         } else if (name.equals("album")) {             album = readalbum(parser);         } else if (name.equals("date")) {             playeduts = integer.parseint(readuts(parser));         } else {             skip(parser);         }     }     return new trackinfo(nowplaying, artist, artistpic, artistmbid, song, songurl,             album, albumpic, playeduts); }  ...  private string readartistpic(xmlpullparser parser) throws xmlpullparserexception, ioexception {     parser.require(xmlpullparser.start_tag, ns, "image");     string artistpic = readtext(parser);     parser.require(xmlpullparser.end_tag, ns, "image");     return artistpic; }  ... 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -