0

我很困惑如何解析这个 xml 并转到下一个标签

RSS --> 频道 --> 项目

像这种结构的xml树,请帮助我完成它。提前致谢。

xml文件的链接

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

安卓java代码:

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

链接在这里

4

2 回答 2

0

这是 xml pull 解析的完整示例,试试这个:

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

于 2016-09-16T04:44:56.723 回答
0

在此解析中,重复解析直到事件是结束文档,当事件类型为开始标记时,检查期望标记,从该示例代码中获取的值在此处获取 what_tagyou_want 标记中的“名称”属性的值。

     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();                    
     }
于 2016-08-19T11:20:51.463 回答