xml は次のようになります (RSS フィードから):
<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();
}