FileDocCategorySizeDatePackage
StackOverflowXmlParser.javaAPI DocAndroid 5.1 API6215Thu Mar 12 22:22:42 GMT 2015com.example.android.networkusage

StackOverflowXmlParser

public class StackOverflowXmlParser extends Object
This class parses XML feeds from stackoverflow.com. Given an InputStream representation of a feed, it returns a List of entries, where each list element represents a single entry (post) in the XML feed.

Fields Summary
private static final String
ns
Constructors Summary
Methods Summary
public java.util.Listparse(java.io.InputStream in)


    // We don't use namespaces

           
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    
private com.example.android.networkusage.StackOverflowXmlParser$EntryreadEntry(org.xmlpull.v1.XmlPullParser parser)

        parser.require(XmlPullParser.START_TAG, ns, "entry");
        String title = null;
        String summary = null;
        String link = null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("title")) {
                title = readTitle(parser);
            } else if (name.equals("summary")) {
                summary = readSummary(parser);
            } else if (name.equals("link")) {
                link = readLink(parser);
            } else {
                skip(parser);
            }
        }
        return new Entry(title, summary, link);
    
private java.util.ListreadFeed(org.xmlpull.v1.XmlPullParser parser)

        List<Entry> entries = new ArrayList<Entry>();

        parser.require(XmlPullParser.START_TAG, ns, "feed");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the entry tag
            if (name.equals("entry")) {
                entries.add(readEntry(parser));
            } else {
                skip(parser);
            }
        }
        return entries;
    
private java.lang.StringreadLink(org.xmlpull.v1.XmlPullParser parser)

        String link = "";
        parser.require(XmlPullParser.START_TAG, ns, "link");
        String tag = parser.getName();
        String relType = parser.getAttributeValue(null, "rel");
        if (tag.equals("link")) {
            if (relType.equals("alternate")) {
                link = parser.getAttributeValue(null, "href");
                parser.nextTag();
            }
        }
        parser.require(XmlPullParser.END_TAG, ns, "link");
        return link;
    
private java.lang.StringreadSummary(org.xmlpull.v1.XmlPullParser parser)

        parser.require(XmlPullParser.START_TAG, ns, "summary");
        String summary = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "summary");
        return summary;
    
private java.lang.StringreadText(org.xmlpull.v1.XmlPullParser parser)

        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    
private java.lang.StringreadTitle(org.xmlpull.v1.XmlPullParser parser)

        parser.require(XmlPullParser.START_TAG, ns, "title");
        String title = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "title");
        return title;
    
private voidskip(org.xmlpull.v1.XmlPullParser parser)

        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                    depth--;
                    break;
            case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }