gpt4 book ai didi

Android SAX 解析器没有从标签之间获取全文

转载 作者:IT老高 更新时间:2023-10-28 23:08:56 27 4
gpt4 key购买 nike

我创建了自己的 DefaultHandler 来解析 rss 提要,并且对于大多数提要来说它工作正常,但是,对于 ESPN,由于 ESPN 格式化它的 url 的方式,它正在切断部分文章 url。来自 ESPN 的完整文章 URL 示例。

http://sports.espn.go.com/nba/news/story?id=5189101&campaign=rss&source=ESPNHeadlines

问题是由于某种原因,DefaultHandler 字符方法只能从包含上述 url 的标签中获取。

http://sports.espn.go.com/nba/news/story?id=5189101

如您所见,它从 & 转义码和之后的 URL 中删除了所有内容。我怎样才能让 SAX 解析器在这个转义码处不切断我的字符串?对于引用。这是我的字符方法..

 public void characters(char ch[], int start, int length) {

String chars = (new String(ch).substring(start, start + length));

try {
// If not in item, then title/link refers to feed
if (!inItem) {
if (inTitle)
currentFeed.title = chars;
} else {
if (inLink)
currentArticle.url = new URL(chars);
if (inTitle)
currentArticle.title = chars;
if (inDescription)
currentArticle.description = chars;
if (inPubDate)
currentArticle.pubDate = chars;
if (inEnclosure) {
}
}
} catch (MalformedURLException e) {
Log.e("RSSReader", e.toString());
}
}

罗伯·W.

最佳答案

As you can see, it's cutting everything off the url from the ampersand escape code and after.

来自 documentation characters() 方法:

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

当我编写 SAX 解析器时,我使用 StringBuilder 来附加所有传递给 characters() 的内容:

public void characters (char ch[], int start, int length) {
if (buf!=null) {
for (int i=start; i<start+length; i++) {
buf.append(ch[i]);
}
}
}

然后在 endElement() 中,我获取 StringBuilder 的内容并对其进行处理。这样,如果解析器多次调用 characters(),我不会错过任何内容。

关于Android SAX 解析器没有从标签之间获取全文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2838099/

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