سؤال

The xml looks like this (out of a RSS feed):

<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>

I want to get the following attributes:

img src - holding the article pic

the article gist at the end of the tag but when running I get a NullPointer Ex.

All the rest (outside the CDATA section work just fine...)

the code I used:

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();
            }
هل كانت مفيدة؟

المحلول

This is what I did:

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

Is there a more elegant way of doing this???

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top