0

I am confused that how to parse this xml and go to next tag

rss --> channel --> item

The xml tree like this structure , please help me through it. Thanks in advance.

link of xml file

<rss>
<channel>
    <item>
        <link> </link>
        <title> </title>
    </item>
</channel>

android java code :

public class RssXmlParser {
private static final String ns = null;
public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
    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 List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Entry> entries = new ArrayList<Entry>();

    parser.require(XmlPullParser.START_TAG, ns, "channel");

    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("item")) {
            entries.add(readEntry(parser));
        } else {
            skip(parser);
        }
    }
    return entries;
}


public static class Entry {
    public final String url;
    public final String title;

    private Entry(String url,String title) {
        this.url = url;
        this.title=title;
    }
}
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "item");
    String title = 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("link")) {
            link = readLink(parser);
        } else {
            skip(parser);
        }
    }
    return new Entry(link,title);
}
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "title");
    String title = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "title");
    return title;
}
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
    String link = "";
    parser.require(XmlPullParser.START_TAG, ns, "link");
    link = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "link");
    return link;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    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;
        }
    }
}
}

link is here

1
  • is pull parsing a requirement? It is know to be difficult to use and produce code that is hard to unwieldy and hard to maintain Commented Aug 28, 2016 at 20:21

2 Answers 2

0

In this parsing repeat the parsing till event is end document and when event type is start tag then check desire tag the get value from that sample code is here get value of a 'Name' attribute in what_tagyou_want tag.

     int event = myparser.getEventType();
     while (event != XmlPullParser.END_DOCUMENT) 
     {
        String name=myparser.getName();

        switch (event)
        {
           case XmlPullParser.START_TAG:

               if(name.equals("What_tagyou_want"))
               {
                     namexml= myparser.getAttributeValue(null,"Name")+" ";//get values from here
                }
                    break;

           case XmlPullParser.END_TAG:

           break;

           default: break;
        }        
        event = myparser.next();                    
     }
Sign up to request clarification or add additional context in comments.

Comments

0

here is complete example of xml pull parsing try this:

https://androidgarden.wordpress.com/2016/09/02/android-xml-pull-parsing-from-sd-card-assets/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.