2014-03-03 49 views
1

獲得從CDATA段屬性的XML看起來是這樣的(出一個RSS飼料):Android的 - 使用XMLPullParser

<description> 
<![CDATA[ 
<div><a href='articleURL'><img src='pic.jpg' alt='blah blah' title='blah blah' 
border='0' width='100' height='56'></a></div> 
]]> 
gist of the article..... 
</description> 

我想獲得以下屬性:

IMG SRC - 保持製品圖片

該文章要求在標籤的末尾,但在運行時,我得到一個NullPointer Ex。

其餘全部(CDATA部分工作就好外...)

我使用的代碼:

class BackgroundParser extends AsyncTask<String, String, Integer>{ 
    int headlineCount = 0; 
    String headlineTitle = ""; 
    Bitmap pic = null; 
    String xmlDate = ""; 
    String gist = ""; 
    String articleUrl = ""; 
    @Override 
    protected Integer doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     try { 
      URL rssFeed = new URL(params[0]); 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      XmlPullParser parser = factory.newPullParser(); 
      InputStream is = rssFeed.openStream(); 
      parser.setInput(is, null); 
      boolean item = false; 
      boolean title = false; 
      boolean date = false; 
      boolean description = false; 
      boolean link = false; 
      String tagName; 
      int eventType = parser.getEventType(); 
      while(eventType!=XmlPullParser.END_DOCUMENT){ 
       if(eventType==XmlPullParser.START_TAG){ 
        tagName = parser.getName(); 
        if(item){ 
         if(tagName.equals("title"))title = true; 
         if(tagName.equals("description")){ 
          String img = parser.getAttributeValue(null, "img src"); 
          Log.i("Info", img); 
          pic = getBitmapFromURL(img); 

         } 
         if(tagName.equals("pubDate"))date = true; 
         if(tagName.equals("description"))description = true; 
         if(tagName.equals("link"))link = true; 
        } 
        else{ 
         if(tagName.equals("item"))item = true; 
        } 
       } 
       if(eventType==XmlPullParser.END_TAG){ 
        tagName = parser.getName(); 
        if(tagName.equals("item")){ 
         item = false; 
         headlines.add(new Headline(headlineTitle,xmlDate,pic,gist,articleUrl)); 
         headlineTitle = null; xmlDate = null; pic = null; gist = null; articleUrl = null; 
         headlineCount++; 
        } 
       } 
       if(eventType==XmlPullParser.TEXT){ 
        if(title){ 
         headlineTitle = parser.getText(); 
         Log.i("Info", headlineTitle); 
         title = false; 
        } 
        if(date){ 
         xmlDate = parser.getText(); 
         Log.i("Info", xmlDate); 
         date = false; 
        } 
        if(description){ 
         gist = parser.getText(); 
         Log.i("info",gist); 
         description = false; 
        } 
        if(link){ 
         articleUrl = parser.getText(); 
         Log.i("info", articleUrl); 
         link = false; 
        } 
       } 
       eventType = parser.next(); 
      } 

回答

1

這是我做過什麼:

if(tagName.equals("description")){ 
    int token = parser.nextToken(); 
    while(token!=XmlPullParser.CDSECT){ 
    token = parser.nextToken(); 
    } 
    String cdata = parser.getText(); 
    Log.i("Info", cdata); 
    String result = cdata.substring(cdata.indexOf("src='")+5, cdata.indexOf("jpg")+3); 
    Log.i("Info", result); 
    pic = getBitmapFromURL(result); 
} 

有一個更優雅的方式做到這一點?