简体   繁体   中英

XML parser getting null

I try to parse XML document and add the result to list. I'm getting null every time. I have no idea what i am doing wrong. The reading is outside the main Thread, and at the end it call mathod that send Intent to another Activity. You can look at the XML from the URL in the code. Thanks for answering. This is my code:

    t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL(
                        "http://www.ynet.co.il/Integration/StoryRss4880.xml");
                XmlPullParserFactory factory = XmlPullParserFactory
                        .newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xpp = factory.newPullParser();
                xpp.setInput(getInputStream(url), "UTF_8");
                boolean insideItem = false;
                int eventType = xpp.getEventType();
                String l = null;
                String tit = null;
                String p = null;
                while (eventType != XmlPullParser.END_DOCUMENT) {                       
                    if (eventType == XmlPullParser.START_TAG) {
                        if (xpp.getName().equalsIgnoreCase("item")) {
                            insideItem = true;
                            if (xpp.getName().equalsIgnoreCase("title")) {
                                if (insideItem) {
                                    l = xpp.nextText();
                                }
                            } else if (xpp.getName().equalsIgnoreCase(
                                    "link")) {
                                if (insideItem) {
                                    tit = xpp.nextText();
                                }
                            } else if (xpp.getName().equalsIgnoreCase(
                                    "description")) {
                                if (insideItem) {
                                    String array[] = xpp.nextText().split(
                                            "img src='");
                                    String array1[] = array[1]
                                            .split("' alt");
                                    p = array1[0];
                                }
                            }
                            MyArticle.getInstance().getmArticle()
                                    .add(new Article(l, tit, p));
                        }
                    } else if (eventType == XmlPullParser.END_TAG
                            && xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = false;
                    }
                    eventType = xpp.next();
                }
                if (eventType == XmlPullParser.END_DOCUMENT) {                  
                        try {
                            t.join();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        GoTOActivity(context);

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    t.start();

The problem is with your inner parsing loop. You test if xpp.getName() is equal to "item", and if it is then you set insideItem to true and then immediately test if xpp.getName() is equal to "title":

if (xpp.getName().equalsIgnoreCase("item")) {
    insideItem = true;
    if (xpp.getName().equalsIgnoreCase("title")) {
        if (insideItem) {
            l = xpp.nextText();
        }
    }

It will never be equal to "title" in this circumstance, because you just tested it and it was equal to "item". You need to read to the next XML start tag before testing for "title", like this:

while (eventType != XmlPullParser.END_DOCUMENT) {                       
    if (eventType == XmlPullParser.START_TAG) {
        if (xpp.getName().equalsIgnoreCase("item")) {
            insideItem = true;
            tit = null; l = null; p = null; // clear variables
        } else if (xpp.getName().equalsIgnoreCase("title")) {
            if (insideItem) {
                tit = xpp.nextText();
            }
        } else if (xpp.getName().equalsIgnoreCase("link")) {
            if (insideItem) {
                l = xpp.nextText();
            }
        } else if (xpp.getName().equalsIgnoreCase("description")) {
            if (insideItem) {
                String array[] = xpp.nextText().split(
                        "img src='");
                String array1[] = array[1]
                        .split("' alt");
                p = array1[0];
            }
        }
    } else if (eventType == XmlPullParser.END_TAG
            && xpp.getName().equalsIgnoreCase("item")) {
        insideItem = false;                         
        MyArticle.getInstance().getmArticle()
                .add(new Article(l, tit, p));                            
    }
    eventType = xpp.next();
}

Also note that the code to add a new article goes where you detect an "item" close tag. Finally, I assume that tit is title and l is link, in which case you need to swap them around so tit is set when you read the title and vice versa. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM