gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:52 25 4
gpt4 key购买 nike

有什么方法可以删除 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"));

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

最佳答案

这将完全避免 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);

关于KML 的 Java API (JAK) - 删除 kml 中额外的 ns2 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31358331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com