tags:

views:

106

answers:

1

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"&gt;
</code>

and here is what we want to replace with:

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

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

+1  A: 

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.

Alphie