2

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();
            }
4

1 回答 1

2

这就是我所做的:

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);
}

有没有更优雅的方式来做到这一点???

于 2014-03-03T14:21:12.337 回答