2015-10-19 44 views
0

我试图从rss xml文件中检索图像链接。图像链接位于名为src的属性中。无论我做什么,我都会收到nullpointerException或其他错误。如何使用xml pullparser检索src属性

的xml文件看起来是这样的:

<item> 
      <title><![CDATA[מרואן ברגותי פרסם מאמר מסית ב"גרדיאן" בשיא הפיגועים, וקיבל אזהרה בלבד]]></title> 
      <description><![CDATA[<div><a href='http://www.ynet.co.il/articles/0,7340,L-4712918,00.html'><img src='http://images1.ynet.co.il/PicServer3/2013/07/30/4764329/18489290991962183103no.jpg' alt='צילום: AP' title='צילום: AP' border='0' width='100' height='56'></a></div>רב-המרצחים הפלסטיני שעמד מאחורי שורה ארוכה של פיגועי טרור הועמד לדין משמעתי בעקבות מאמר מסית שפרסם ב"גרדיאן" הבריטי בשיא הפיגועים בחודש האחרון. בין היתר כתב כי "לא נחיה עם הכיבוש ולא ניכנע לו"]]></description> 
      <link><![CDATA[http://www.ynet.co.il/articles/0,7340,L-4712918,00.html]]></link> 
      <pubDate><![CDATA[Sun, 18 Oct 2015 15:38:00 +0300]]></pubDate> 
      <guid><![CDATA[http://www.ynet.co.il/articles/0,7340,L-4712918,00.html]]></guid> 
      <tags><![CDATA[מרואן ברגותי]]></tags> 
     </item> 
     <item> 

我的代码是:

while(eventype!=XmlPullParser.END_DOCUMENT) 
        { 
         if(eventype==XmlPullParser.START_TAG) 
         { 
          tagname=parser.getName(); 

          if(item) 
          { 
           if(tagname.equals("title")) 
             title=true; 
           if(tagname.equals("link")) 
            link=true; 
           if(tagname.equals("description")) 
           { 

            while(eventype!=XmlPullParser.START_TAG && tagname.equals("img")==false) 
              eventype=parser.next(); 

            String imgrext= parser.getText(); 
            Log.i("imag text is",imgrext); 
            description=true; 

           } 

          } 

          else//not item 
          { 
           if(tagname.equals("item")) 
            item=true; 
          } 
         } 
         if(eventype==XmlPullParser.END_TAG) 
         { 
          tagname=parser.getName(); 
          if(tagname.equals("item")) 
           item=false; 
         } 
         if(eventype==XmlPullParser.TEXT) 
         { 

          if(title) 
          { 
           coteret=parser.getText(); 
           coteretfilled=true; 
           title=false; 
          } 
          if(link) 
          { 
           _link=parser.getText(); 
           linkfiled=true; 
           link=false; 
          } 
          /*if(description) 
          { 

           String cdata = parser.getText(); 
           Log.i("Info", cdata); 

           // result = cdata.substring(cdata.indexOf("<img"), cdata.indexOf("alt")); 
           Log.i("Info", result); 
          // Log.i("Info", cdata.indexOf("alt")+""); 
           description=false; 
          } 
          */ 


          if(coteretfilled && linkfiled) 
          { 
           TitlesVieLinks titlink=new TitlesVieLinks(coteret, _link); 
           tl.add(titlink); 
           coteretfilled=false; 
           linkfiled=false; 
          } 
         } 

         eventype = parser.next(); 

        }//end of while loop 

我用getAttributeCount但它返回-1

回答

0
if(description) 
{ 
    String srcdata = parser.getText(); 
    int idxStart = srcdata.indexOf("src="); 
    if (idxStart > 0) 
    { 
     int idxEnd = Math.max(srcdata.indexOf(".jpg"), crcdata.indexOf(".png")); //returns the greater of 2 int values 
      if (idxEnd > 0) 
      { 
       result = srcdata.substring(idxStart+5, idxEnd+4); 
      } 
    } 
    else 
     result=""; 

}