SAX解析器返回标题的第一个字符
我正在构建一个使用SAX解析器解析RSS源的Android应用程序。当我打印标题时,我只能看到它的第一个字符。SAX解析器返回标题的第一个字符
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);
的characters()
功能可以在开始标签后,多次和结束标记之前被调用。每次它在一次调用中给你一些字节,但不一定是所有的字节。
因此,您可以在onStartTag
函数中创建StringBuilder
。然后在每次输入字符函数时附加到该StringBuilder。然后,当您为此标记输入onEndTag
函数时,您的StringBuilder将包含所有字符。 什么附加由characters
功能的offset
和length
参数确定
只是尝试添加字符串构建,但仍返回第一个字符,我将添加我所做的代码更改 – happyhardik 2011-12-26 14:18:26
我忘了评论currentstate = 0行,在评论字符函数中的TITLE字段并在标记End中添加该行后,它工作。 – happyhardik 2011-12-26 15:37:42
我测试了它更多的,如果标题没有了“(引号)它的工作原理。 – happyhardik 2011-12-26 12:03:56