开发者

Sanitizing KML Stream to change xml namespace

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 FileIn开发者_开发百科putStream 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜