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>&quot;The Color of the Night is not always Black II_(Explored Highest Position #1)&quot; 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); 
+0

我测试了它更多的,如果标题没有了“(引号)它的工作原理。 – happyhardik 2011-12-26 12:03:56

characters()功能可以在开始标签后,多次和结束标记之前被调用。每次它在一次调用中给你一些字节,但不一定是所有的字节。

因此,您可以在onStartTag函数中创建StringBuilder。然后在每次输入字符函数时附加到该StringBuilder。然后,当您为此标记输入onEndTag函数时,您的StringBuilder将包含所有字符。 什么附加由characters功能的offsetlength参数确定

+0

只是尝试添加字符串构建,但仍返回第一个字符,我将添加我所做的代码更改 – happyhardik 2011-12-26 14:18:26

+0

我忘了评论currentstate = 0行,在评论字符函数中的TITLE字段并在标记End中添加该行后,它工作。 – happyhardik 2011-12-26 15:37:42