Java API for KML (JAK) - Remove optional ns2 annotation in kml

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

This is the kml example I get 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>

      

I want 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 greatly appreciated! Thank!

+3


source to share


2 answers


This completely removes 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);

      

+2


source


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);

      

He 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 will look for other changes I could make to the jak code.

0


source







All Articles