用于KML的Java API(JAK) - 以kml删除额外的ns2注释

时间:2015-07-11 14:43:33

标签: java namespaces marshalling kml jak

有没有办法在KML文件中删除额外的名称空间前缀(即ns2)?

这是我从代码中收到的kml的一个例子:

<?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>

我想要的是这样的:

<?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>

这是我的java代码:

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

非常感谢任何帮助!谢谢!

2 个答案:

答案 0 :(得分:2)

这将完全避免kml元素的前缀:

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

答案 1 :(得分:0)

你能试试这段代码吗?

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

它为我生产

<?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>

如果没有,我将寻找我可能对jak代码做出的其他更改。