3

经过几个小时的搜索和调试后,我仍然被困在同一个地方,Eclipse 并没有帮助我。

我试图解析这个 RSS 提要

这很简单。连接完成,我将 InputStream 转换为字符串,这很好用。

当我尝试解析获得的字符串时,我没有错误或警告,但我尝试拥有的值为空。这是XML过程

public class XmlOperations {
private static final String ns = null;

public List parse(String in) throws XmlPullParserException, IOException {
    try {

        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(new StringReader(in));
        parser.nextTag();
        return readFeed(parser);
    } finally {
        // 
    }
}

private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List entries = new ArrayList();
    // first xml balise
    parser.require(XmlPullParser.START_TAG, ns,"rss"); 
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        Log.w(TAG, name); // output channel the first time
        // Starts by looking for the item tag
        if (name.equals("item")) {
            **Log.w(TAG, "Never get in here" )**
          entries.add(readEntry(parser));
        } else {
            skip(parser);
            Log.w(TAG, "lala");
        }
    }  
    return entries;
}



// Parses the contents of an item. If it encounters a title, description, or link tag, hands them off
// to their respective "read" methods for processing. Otherwise, skips the tag.
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "item");
    String title = null;
    String description = 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("description")) {
            description = readDescription(parser);
        } else if (name.equals("link")) {
            link = readLink(parser);
        } else {
            skip(parser);
        }
    }
    return new Entry(title, description, link);
}

// Processes title tags in the feed.
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;
}

// Processes link tags in the feed.
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "link");
    String link = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "link");
    return link;
}

// Processes summary tags in the feed.
private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "description");
    String description = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "description");
    //Log.d("vALUE = ", summary);
    return description;
}

// For the tags title and description, extracts their text values.
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;
        }
    }
 }

我想错误出在其中一种解析方法中。

在另一个班级我有这个

XmlOperations xml = new XmlOperations();
        try {
            entrieses = xml.parse(result);
            String lol = null;
            for(Entry item : entrieses) {
                //Toast.makeText(getApplicationContext(), item.title, Toast.LENGTH_SHORT).show();
                lol = item.link.toString();
            }
            tv.setText("Pin " + lol + " gouin");

entreses 是一个条目列表。仍然等于 null :(

编辑:有了日志猫,我有频道 lala,仅此而已

4

2 回答 2

1

我终于知道了。我的代码是正确的,我的 XML 理解不是。XML 是这样的

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

我的代码进入频道,然后进入下一个频道,但它不存在。我只需要在循环之前添加 parser.Next()我的reedFeed()函数来传递通道标签并直接进入项目标签。它有效:)

于 2012-11-16T18:34:10.560 回答
0

我认为您的readFeed()函数中的行需要while (parser.next() != XmlPullParser.END_DOCUMENT) {代替while (parser.next() != XmlPullParser.END_TAG) {,因为否则它几乎会立即返回。

于 2012-11-16T18:22:50.223 回答