如何获得另一个xml android内的标签

时间:2015-03-21 00:58:31

标签: java android xml xml-parsing

这是我的xml代码,我想从这个xml代码中获取items

问题是我不知道如何直接进入'item',因为当你看到'item'进入'channel'时:http://www.scarlett-fan.com/feed/

这是我的java代码的一部分

// get initial eventType
    int eventType = xpp.getEventType();

    // Loop through pull events until we reach END_DOCUMENT
    while (eventType != XmlPullParser.END_DOCUMENT) {
        // Get the current tag

        String tagname = xpp.getName();


        // React to different event types appropriately
        switch (eventType) {
        case XmlPullParser.START_TAG:

            if (tagname.equalsIgnoreCase(KEY_SITE)) {
                // If we are starting a new <site> block we need
                //a new StackSite object to represent it
                curStackSite = new StackSite();
            }
            break;

        case XmlPullParser.TEXT:
            //grab the current text so we can use it in END_TAG event
            curText = xpp.getText();
            break;

        case XmlPullParser.END_TAG:


            if (tagname.equalsIgnoreCase(KEY_SITE)) {
                // if </site> then we are done with current Site
                // add it to the list.
                stackSites.add(curStackSite);
            } else if (tagname.equalsIgnoreCase(KEY_NAME)) {
                // if </name> use setName() on curSite
                curStackSite.setName(curText);
                Log.i("ghazi","ghazi"+curText);
            } else if (tagname.equalsIgnoreCase(KEY_LINK)) {
                // if </link> use setLink() on curSite
                curStackSite.setLink(curText);
            } else if (tagname.equalsIgnoreCase(KEY_ABOUT)) {
                // if </about> use setAbout() on curSite
                curStackSite.setAbout(curText);
            }
            break;

        default:
            break;
        }
        //move on to next iteration
        eventType = xpp.next();
    }
} catch (Exception e) {
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

    case XmlPullParser.START_TAG:
        // Locate an item
        if (tagname.equalsIgnoreCase("item")) {
            // Mark a flag so we know we're in an item
            inItem = true;
            // Create a new item to build
            currentItem = new Item();
        } else if (inItem && tagname.equalsIgnoreCase("title")) {
            // Mark our current element so we know to grab the text
            currentItemElement = ItemElement.Title;
        }
        break;
    case XmlPullParser.TEXT:
        if (inItem && currentItemElement == ItemElement.Title) {
            // Grab the title text
            currentItem.title = xpp.getText();
        }
        break;
    case XmlPullParser.END_TAG: {
        // Check if we finished an item
        if (inItem && tagname.equalsIgnoreCase("item")) {
            // Clear our flag tracking if we're in an item
            inItem = false;
            // Add the item to whatever list of items we're building
            items.add(currentItem);
        } else {
            currentItemElement = null;
        }
        break;

// ...

public enum ItemElement {
    Title
}