【问题标题】:SAX Parser returns first character of the titleSAX Parser 返回标题的第一个字符
【发布时间】:2011-12-26 11:44:54
【问题描述】:

我正在构建一个使用 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>&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);

【问题讨论】:

  • 我对它进行了更多测试,如果标题没有“(引号)它可以工作。

标签: java android parsing rss sax


【解决方案1】:

characters() 函数可以在开始标记之后和结束标记之前多次调用。每次它都会为您带来一些字节,但不一定会在一次调用中一次获得所有字节。

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

【讨论】:

  • 刚刚尝试添加String Builder,但它仍然返回第一个字符,我将添加我所做的代码更改。
  • 我忘记注释 currentstate=0 行,在为字符函数中的 TITLE 字段注释该行并将该行添加到标签 End 之后,它起作用了。谢谢!
猜你喜欢
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
相关资源
最近更新 更多