3

Is there any way to remove the extra namespace prefix (i.e. ns2) in KML file?

This is an example of the kml I receive from my code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:kml xmlns="http://www.google.com/kml/ext/2.2"     xmlns:ns2="http://www.opengis.net/kml/2.2" xmlns:ns3="http://www.w3.org/2005/Atom" xmlns:ns4="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
    <ns2:Placemark>
        <ns2:name>London, UK</ns2:name>
        <ns2:open>1</ns2:open>
        <ns2:Point>
            <ns2:coordinates>-0.126236,51.500152</ns2:coordinates>
        </ns2:Point>
    </ns2:Placemark>
</ns2:kml>

What I want is something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Placemark>
        <name>London, UK</name>
        <open>true</open>
        <Point>
            <altitudeMode>clampToGround</altitudeMode>
            <coordinates>-0.126236,51.500152</coordinates>
        </Point>
    </Placemark>
</kml>

This is my java code:

final Kml kml = new Kml();    
kml.createAndSetPlacemark()
.withName("London, UK").withOpen(Boolean.TRUE)
.createAndSetPoint().addToCoordinates(-0.126236, 51.500152);
//marshals to console
kml.marshal();
//marshals into file
kml.marshal(new File("output.kml"));

Any help is much appreciated! Thanks!

  • possible duplicate of [Java: Marshalling Object -- Removing extra ns2 annotation in xml](http://stackoverflow.com/questions/7014746/java-marshalling-object-removing-extra-ns2-annotation-in-xml) – geocodezip Jul 12 '15 at 01:21
  • I did not get any solution from there ... – Ashik Mahmud Jul 12 '15 at 06:01

2 Answers2

3

This will fully avoid the prefixes on kml elements:

Marshaller marshaller = JAXBContext.newInstance(new Class[]{Kml.class}).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper()
{
    @Override
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix)
    {
        return namespaceUri.matches("http://www.w3.org/\\d{4}/Atom") ? "atom"
                : (
                namespaceUri.matches("urn:oasis:names:tc:ciq:xsdschema:xAL:.*?") ? "xal"
                        : (
                        namespaceUri.matches("http://www.google.com/kml/ext/.*?") ? "gx"
                                : (
                                namespaceUri.matches("http://www.opengis.net/kml/.*?") ? ""
                                        : (
                                        null
                                        )
                                )
                        )
                );
    }
});
marshaller.marshal(kml, file);
Peter C.
  • 181
  • 1
  • 5
0

Can you try this code

Marshaller m =  JAXBContext.newInstance(new Class[] { Kml.class }).createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
m.setProperty( Marshaller.JAXB_FRAGMENT, false );
m.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
final Kml kml = new Kml();    
kml.createAndSetPlacemark()
.withName("London, UK").withOpen(Boolean.TRUE)
.createAndSetPoint().addToCoordinates(-0.126236, 51.500152);
StringWriter sw = new StringWriter();
m.marshal(kml,sw);
String s1 = sw.toString();
System.out.println(s1);

It produces for me

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kml:kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <kml:Placemark>
        <kml:name>London, UK</kml:name>
        <kml:open>1</kml:open>
        <kml:Point>
            <kml:coordinates>-0.126236,51.500152</kml:coordinates>
        </kml:Point>
    </kml:Placemark>
</kml:kml>

If not, I'll hunt for other changes I might have made to the jak code.

ZiglioUK
  • 2,573
  • 4
  • 27
  • 32
  • Unfortunately this doesn't solve Google Maps problem. Their implementation expects no prefixes at all it seems, including **kml:** It seems that source package have to have different annotation: [Remove namespace prefix while JAXB marshalling](http://stackoverflow.com/a/22755785/194624) – Surge Jan 27 '16 at 14:15
  • I didn't know you could import KML into Google Maps, or didn't remember. Anyway, I'm pretty sure at a certain point I did manage to remove prefixes completely by playing with XMLStreamWriter properties https://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLStreamWriter.html – ZiglioUK Jan 27 '16 at 20:20