SAX 解析器返回标题的第一个字符

发布于 2024-12-23 08:56:18 字数 1140 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-12-30 08:56:18

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

因此,您可以在onStartTag函数中创建一个StringBuilder。然后每次输入字符函数时附加到该 StringBuilder。然后,当您为此标记输入 onEndTag 函数时,您的 StringBuilder 将拥有所有字符。
追加的内容由 characters 函数的 offsetlength 参数确定

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 in onStartTag function. Then append to that StringBuilder every time you enter the characters function. Then when you enter onEndTag function for this tag your StringBuilder will have all the characters.
What to append is determined by the offset and length parameters of the characters function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文