SAX 解析器返回标题的第一个字符
我正在构建一个使用 SAX 解析器解析 RSS 提要的 Android 应用程序。当我打印标题时,我只得到它的第一个字符。
public void characters(char ch[], int start, int length)
{
String theString = new String(ch,start,length);
//Log.i("RSSReader","characters[" + theString + "]");
switch (currentstate)
{
case RSS_TITLE:
_item.setTitle(theString);
Log.i("tag","Length:"+length);
currentstate = 0;
break;
}
}
这里,日志显示长度始终为 1。这甚至发生在描述中。在它与另一个提要一起工作之前,现在当我切换到另一个提要时它遇到了麻烦。 这是示例标题标签:
<title>"The Color of the Night is not always Black II_(Explored Highest Position #1)" by xris74</title>
我在屏幕上打印的是“即(引用)
提前致谢!
编辑1: 我添加了字符串生成器。
1. Added new string builder variable:
StringBuilder newString;
2. On Element Start, if its title
newString = new StringBuilder();
3. On Element End, if its title
_item.setTitle(newString.toString());
4. In characters function, if its title tag, than:
newString.append(ch,start,length);
I am building an Android app that parses an RSS feed using SAX parser. When I print the title, I only get the first character of it.
public void characters(char ch[], int start, int length)
{
String theString = new String(ch,start,length);
//Log.i("RSSReader","characters[" + theString + "]");
switch (currentstate)
{
case RSS_TITLE:
_item.setTitle(theString);
Log.i("tag","Length:"+length);
currentstate = 0;
break;
}
}
Here, the log says that the length is 1 always. This even happens with the description. Before it was working with another feed, now its got trouble when I switched to another feed.
Here is example title tag:
<title>"The Color of the Night is not always Black II_(Explored Highest Position #1)" by xris74</title>
What I get printed on screen is " that is (quote)
Thanks in advance!
EDIT 1:
I added up string builder.
1. Added new string builder variable:
StringBuilder newString;
2. On Element Start, if its title
newString = new StringBuilder();
3. On Element End, if its title
_item.setTitle(newString.toString());
4. In characters function, if its title tag, than:
newString.append(ch,start,length);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
characters()
函数可以在开始标记之后和结束标记之前调用多次。每次它都会为您带来一些字节,但不一定会在一次调用中一次带来所有字节。因此,您可以在
onStartTag
函数中创建一个StringBuilder
。然后每次输入字符函数时附加到该 StringBuilder。然后,当您为此标记输入onEndTag
函数时,您的 StringBuilder 将拥有所有字符。追加的内容由
characters
函数的offset
和length
参数确定The
characters()
function can be called many times after the start tag and before the end tag. Each time it brings you some bytes but not necessarily all the bytes at once in one call.So you can create a
StringBuilder
inonStartTag
function. Then append to that StringBuilder every time you enter the characters function. Then when you enteronEndTag
function for this tag your StringBuilder will have all the characters.What to append is determined by the
offset
andlength
parameters of thecharacters
function