清理 KML 流以更改 xml 命名空间

发布于 2024-08-07 06:11:37 字数 685 浏览 1 评论 0原文

使用兼容的解析器 JAK 解析 Google 地球生成的 KML 时会出现一些奇怪的行为。 GE Kml 生产系统未将解析器预期的名称空间写入正确的名称空间,并且将此 KML 读回另一个 Java 应用程序时验证失败。

我们在读取流时对其进行修改,并将出现的任何其他命名空间替换为正确的命名空间。这只需要在文件开头附近完成一次。尝试的方法是将前几行解析为字符串,执行搜索和替换,创建字符串流并使用 SequenceInputStream 将 FileInputStream 的“其余部分”连接到 StringStream。然而这不起作用。任何想法将不胜感激。

以下是 KML xmlns 片段的开头:

<code>
    <kml xmlns="http://earth.google.com/kml/2.2">
</code>

以下是我们要替换的内容:

<code>
    <kml xmlns="http://www.opengis.net/kml/2.2">
</code>

序列流返回错误的可用值(仅适用于序列中的第一个流),导致解析器失败。

There is some strange behaviour in parsing KML produced by Google Earth using a compliant parser, JAK. The expected namespace by the parser is not written as the correct one by the GE Kml production system and when reading this KML back into another Java application validation fails.

We are fiddling the stream as it is read and replacing occurences of any other namespace with the proper namespace. This only needs to be done once near the beginning of the file. The attempted approach was to parse in the first few lines to a string, perform a search-and-replace, create stringstream and concatenate the "rest" of the FileInputStream to the StringStream using a SequenceInputStream. This however does not work. Any ideas will be appreciated.

Here is what the KML xmlns fragment starts with:

<code>
    <kml xmlns="http://earth.google.com/kml/2.2">
</code>

and here is what we want to replace with:

<code>
    <kml xmlns="http://www.opengis.net/kml/2.2">
</code>

The sequence stream returns a faulty available value (only for the 1st stream in the sequence) causing the parser to fail.

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

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

发布评论

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

评论(1

迟月 2024-08-14 06:11:37

SequenceInputStream 以一种不同寻常的方式工作。它将两个流连接成一个虚拟流。 SequenceInputStream 的 available() 方法将返回当前流的长度,而不是按预期组合的所有流的长度。

您应该使用 StringBuffer 代替 SequenceInputStream 来读入文件并在读入数据时进行所需的任何更改。StringBuffer 提高了添加字符串对象的性能。如果最后需要流输出,请将 StringBuffer.toString() 方法解析为 StringStream。

SequenceInputStream works in an unusual fashion. It joins the two streams into a virtual stream. The available() method for a SequenceInputStream will return the length for the current stream, not the all streams combined as may be expected.

Instead of a SequenceInputStream, you should use a StringBuffer to read the file into and make any changes you need to, as data is read in. StringBuffer improves the performance of adding string objects. Parse the StringBuffer.toString() method to a StringStream if you require a stream output at the end.

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