Android 中的 DefaultHandler 类不会从 xml 读取整个字符串

发布于 2024-10-17 21:45:26 字数 1920 浏览 3 评论 0原文

我正在扩展 DefaultHandler 来解析 xml。 我正在解析此页面: http://maps.googleapis .com/maps/api/directions/xml?origin=staten%20island&destination=florida&sensor=false

我正在获取所有折线>点。 除了一个特定的字符串之外,一切正常。 我像这样重写 DefaultHandler 中的字符方法:

public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement) {
    Log.v("Length", length+"");
    currentValue = new String(ch, start, length);
    currentElement = false;
    }

}

当字符串长度为 660 个字符时,长度返回 282。 这是字符串:

qhitFxpifMn@t@vACdMWnnBjeEtI|PlIxNxHhMjLdQ|dAlwA~CxfEfOxRlKO~PdUvOlTnHdJbYl\fVlWq@~p@~gA >gAfKzKbPRpa@bi@|wAnuBjZ|c@rD~FJjP|I|Qzr@~AbKpSJ~OvGdKnPhUbMpN~{@~{@tLhMlZfZnI~Ilh@nh @nNO~GbJjEpGpErHbIfPpDxIvDpKzRvp@rE|MrF|MhKpRbFIr_@jo@LtS~MzYp[bw@hEvJ|FLbLvQzHrJfv@fz@nUxXhAtoAlGzIl >AbwAvMfRx_AnmAlr@z}@jQtTfJxJv]h]buDfqDjFrGbI~K|E~HpFnKlFfLzB|FjElMhCvI|Tw@vDnPzChPx@vFdBhOrh@xlFbNptAvAdMnBMnEjT~EvQhFO~FNj CjFhEthlEbHbk@ bx@fWb^nSvXnSrYxGpKjDvGnDxHnHdR~_A|gCdNz]tEtMfs@zlBbHhQ|jDzoH|r@lzA|Yvk@xClFvJxRhInOhP\hJ|PbIvOxB|E|m@plA~LpUzs@dwAvj@hfA|D|I rDrJzlAriDhFfR~Uv~@ h@j|Afy@{DtFzX|BtOpArLvSjcCv@dNJjEGbFDdDVnElAnIx@xDnC~JbApFb@fDbKb~@Dj^hBvN|BbO|Dl]

这就是 DefaultHandler 返回的内容:

qhitFxpifMn@t@ vACdMWnnBjeEtI|PlIxNxHhMjLdQ|dAlwA~CxfEfOxRlKO~PdUvOlTnHdJbYl\fVlWq@~p@~gAgAfKzKbPRpa@bi@ |wAnuBjZ|c@rD~FJjP|I|Qzr@~AbKpSJ~OvGdKnPhUbMpN~{@~{@tLhMlZfZnI~Ilh@nh@nNO~GbJjEpGpErHbIfPpDxIvDpKzRvp@ rE|MrF|MhKpRbFIr_@jo@LtS~MzYp[bw@hEvJ|FLbLvQzHrJfv@fz@nUxXh`Ato

它停在字符 282 处,我不知道为什么。

I am extending DefaultHandler to parse an xml.
I am parsing this page:
http://maps.googleapis.com/maps/api/directions/xml?origin=staten%20island&destination=florida&sensor=false

I am getting all the polyline>points.
Everything works fine except for one specific string.
I am overriding the characters method in DefaultHandler like this:

public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement) {
    Log.v("Length", length+"");
    currentValue = new String(ch, start, length);
    currentElement = false;
    }

}

The length returns 282 when the string is 660 characters long.
This is the string:

qhitFxpifMn@t@vACdMWnnBjeEtI|PlIxNxHhMjLdQ|dAlwA~CxfEfOxRlKO~PdUvOlTnHdJbYl\fVlWq@~p@~gAgAfKzKbPRpa@bi@|wAnuBjZ|c@rD~FJjP|I|Qzr@~AbKpSJ~OvGdKnPhUbMpN~{@~{@tLhMlZfZnI~Ilh@nh@nNO~GbJjEpGpErHbIfPpDxIvDpKzRvp@rE|MrF|MhKpRbFIr_@jo@LtS~MzYp[bw@hEvJ|FLbLvQzHrJfv@fz@nUxXhAtoAlGzIlAbwAvMfRx_AnmAlr@z}@jQtTfJxJv]h]buDfqDjFrGbI~K|E~HpFnKlFfLzB|FjElMhCvI|Tw@vDnPzChPx@vFdBhOrh@xlFbNptAvAdMnBMnEjT~EvQhFO~FNjCjFhEtHlEbHbk@bx@fWb^nSvXnSrYxGpKjDvGnDxHnHdR~_A|gCdNz]tEtMfs@zlBbHhQ|jDzoH|r@lzA|Yvk@xClFvJxRhInOhP\hJ|PbIvOxB|E|m@plA~LpUzs@dwAvj@hfA|D|IrDrJzlAriDhFfR~Uv~@h@j|Afy@{DtFzX|BtOpArLvSjcCv@dNJjEGbFDdDVnElAnIx@xDnC~JbApFb@fDbKb~@Dj^hBvN|BbO|Dl]

And this is what DefaultHandler returns:

qhitFxpifMn@t@vACdMWnnBjeEtI|PlIxNxHhMjLdQ|dAlwA~CxfEfOxRlKO~PdUvOlTnHdJbYl\fVlWq@~p@~gAgAfKzKbPRpa@bi@|wAnuBjZ|c@rD~FJjP|I|Qzr@~AbKpSJ~OvGdKnPhUbMpN~{@~{@tLhMlZfZnI~Ilh@nh@nNO~GbJjEpGpErHbIfPpDxIvDpKzRvp@rE|MrF|MhKpRbFIr_@jo@LtS~MzYp[bw@hEvJ|FLbLvQzHrJfv@fz@nUxXh`Ato

It stops at character 282 and I don't know why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不即不离 2024-10-24 21:45:26

来自 ContentHandler 接口的文档,它是由DefaultHandler实现的:

SAX 解析器可能会返回单个块中的所有连续字符数据,也可能会将其拆分为多个块

您需要在 StringBuilder 中聚合对此方法的多次调用的数据,然后在 endElement 调用中处理该数据。

From the documentation of the ContentHandler interface, which is implemented by DefaultHandler:

SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks

You need to aggregate the data from multiple calls to this method in a StringBuilder and then handle that data in the endElement call.

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