gpt4 book ai didi

java - 如果下一个标签位于新行上,XmlPullParser 读取 null

转载 作者:行者123 更新时间:2023-11-30 04:05:58 24 4
gpt4 key购买 nike

我正在尝试使用 xmlpullparser 制作 RSS 阅读器。假设我有一个如下所示的 xml 文件:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Title</title>
<description>This is an example of an RSS feed</description>
<link>http://www.someexamplerssdomain.com/main.html</link>
<lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
<pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>
<ttl>1800</ttl>

<item>
<title>Example entry</title>
<description>Here is some text containing an interesting description.</description>
<link>http://www.wikipedia.org/</link>
<guid>unique string per item</guid>
<pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>
</item>

</channel>
</rss>

当我尝试读取它时,它读取 rss 标签,然后在读取 channel 之前,它读取 null。将 channel 标签移动到 rss 标签旁边,如下所示:

<rss version="2.0"><channel>
<title>...

我不再有这个问题,但我不能这样做,因为我不想只读取我的 xml 文件。

这是我正在使用的代码的一部分:

public Feed rss() throws XmlPullParserException, IOException {
matchStart(Tag.rss);
move();
Feed channel = channel();
matchEnd(Tag.rss);

return channel;
}

private Feed channel() throws XmlPullParserException, IOException {
matchStart(Tag.channel);
move();

String title = null;
String link = null;
String description = null;
String pubDate = null;
List<FeedMessage> items = new ArrayList<FeedMessage>();

while(eventType != XmlPullParser.END_TAG) {
if(xmlParser.getEventType() != XmlPullParser.START_TAG) { //Look if we are checking an open tag <tag> if not es</tag> then go to the next one
move();
continue;
}

String name = xmlParser.getName();
switch(name) {
case Tag.title: title = title(); break;
case Tag.link: link = link(); break;
case Tag.description: description = description(); break;
case Tag.pubDate: pubDate = pubDate(); break;
case Tag.item: items.add(item()); break;
default: skip(); break;
}

move();
}

Feed channel = new Feed(title, link, description, pubDate, items);

matchEnd(Tag.channel);

return channel;
}

其中 matchStart 和 matchEnd 调用 xmlParser.require 来检查 START_TAG 和 END_TAG并移动它的定义如下:

void move() throws XmlPullParserException, IOException {
eventType = xmlParser.next();
}

我找不到问题出在哪里,代码或者逻辑有问题吗?

最佳答案

当你有换行符时 next开始 rss 标记之后的事件不是 channel 标记,而是表示该换行符和 channel 标记之前的缩进的 TEXT 事件。您需要继续跳过,直到遇到 <rss> 之后的下一个开始标记事件。 ,就像您已经在 channel() 内的主循环中所做的那样.

关于java - 如果下一个标签位于新行上,XmlPullParser 读取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20717793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com