gpt4 book ai didi

java - DefaultHandler 中的空 uri 和 localName

转载 作者:行者123 更新时间:2023-12-01 15:22:08 25 4
gpt4 key购买 nike

我正在开发一个 RSS 阅读器。运行此代码时,localName 和 uri 为空。我正在解析 RSS 提要。我正在运行以下代码。相同的代码在另一个 android 项目中运行良好。

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {

String name;
if (localName == "" ){
name = qName;
}
else{
name = localName;
}

if (name.equalsIgnoreCase("title")) {
currentPost.setTitle(chars.toString());
}
if (name.equalsIgnoreCase("link")) {
currentPost.setLink(chars.toString());
}
if (name.equalsIgnoreCase("content")
&& currentPost.getContent() == null) {
currentPost.setContent(chars.toString());
}
if (name.equalsIgnoreCase("item")) {
currentPost.setFeed(feed);
Posts.Instance().add(currentPost);
currentPost = new Post();
}
}

最佳答案

根据API

Parameters:

uri - The Namespace URI, or the empty string if theelement has no Namespace URI or if Namespace processing is not being performed.

localName - The local name (without prefix), or the emptystring if Namespace processing is not being performed.

...

The characters method can get called multiple times while inside atag, especially if the element value contains whitespace.

在characters()文档中

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

因此,

When I write SAX parsers, I use a StringBuilder to append everythingpassed to 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]);
}
}
}

Then in endElement(), I take the contents of the StringBuilder and do something with it. That way, if the parser calls characters() several times, I don't miss anything.

信用:https://stackoverflow.com/a/7182648/643500https://stackoverflow.com/a/2838338/643500

编辑::

阅读http://sujitpal.blogspot.com/2008/07/rss-feed-client-in-java.html

关于java - DefaultHandler 中的空 uri 和 localName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744660/

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