使用java导入kml

使用java导入kml,java,kml,jak,Java,Kml,Jak,我正在尝试使用jak导入mkl文件,但出现以下错误: javax.xml.bind.UnmarshaleException:意外元素(uri:)http://earth.google.com/kml/2.2,本地:“kml”)。预期的要素是 ... 然后是一张大名单 还有其他人遇到这个问题吗 代码如下: final Kml kml = Kml.unmarshal(new File("../data/Eemskanaal.kml")); final Placemark place

我正在尝试使用jak导入mkl文件,但出现以下错误:

javax.xml.bind.UnmarshaleException:意外元素(uri:)http://earth.google.com/kml/2.2,本地:“kml”)。预期的要素是 ... 然后是一张大名单

还有其他人遇到这个问题吗

代码如下:

final Kml kml = Kml.unmarshal(new File("../data/Eemskanaal.kml"));
        final Placemark placemark = (Placemark) kml.getFeature();
        Point point = (Point) placemark.getGeometry();
        List<Coordinate> coordinates = point.getCoordinates();
        for (Coordinate coordinate : coordinates) {
            System.out.println(coordinate.getLatitude());
            System.out.println(coordinate.getLongitude());
            System.out.println(coordinate.getAltitude());
        }
final Kml Kml=Kml.unmarshal(新文件(“../data/eemskanal.Kml”);
最终Placemark Placemark=(Placemark)kml.getFeature();
Point=(Point)placemark.getGeometry();
列表坐标=point.getCoordinates();
用于(坐标:坐标){
System.out.println(coordinate.getLatitude());
System.out.println(coordinate.getLongitude());
System.out.println(coordinate.getAltitude());
}
这是kml文件:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
  <name>BU00100107 Verspreide huizen Eemskanaal (ten zuiden)</name>
  <description><![CDATA[description]]></description>
  <Placemark>
    <name>BLA!</name>
    <description><![CDATA[]]></description>
    <styleUrl>#style1</styleUrl>
    <Polygon>
      <outerBoundaryIs>
        <LinearRing>
          <tessellate>1</tessellate>
          <coordinates>
            6.941796,53.314914,0.000000
            6.942705,53.310923,0.000000
            6.952713,53.305394,0.000000
            6.954853,53.300262,0.000000
            6.954239,53.296317,0.000000
            6.962271,53.295483,0.000000
            6.995900,53.287338,0.000000
            6.995013,53.285264,0.000000
            6.996842,53.281429,0.000000
            6.991748,53.278255,0.000000
            6.990729,53.275234,0.000000
            6.988361,53.274477,0.000000
            6.990120,53.271780,0.000000
            6.984540,53.272709,0.000000
            6.984543,53.274393,0.000000
            6.980317,53.274404,0.000000
            6.975829,53.272503,0.000000
            6.974816,53.271125,0.000000
            6.963342,53.271937,0.000000
            6.955082,53.265909,0.000000
            6.945183,53.269634,0.000000
            6.940684,53.273351,0.000000
            6.935942,53.273875,0.000000
            6.934392,53.276351,0.000000
            6.929104,53.272181,0.000000
            6.909544,53.265952,0.000000
            6.908803,53.269015,0.000000
            6.909151,53.278897,0.000000
            6.888166,53.279161,0.000000
            6.887788,53.279639,0.000000
            6.886750,53.280950,0.000000
            6.886729,53.280977,0.000000
            6.888260,53.281856,0.000000
            6.895912,53.286254,0.000000
            6.892976,53.288089,0.000000
            6.891571,53.290803,0.000000
            6.887323,53.298046,0.000000
            6.887729,53.309725,0.000000
            6.887583,53.309816,0.000000
            6.888683,53.311891,0.000000
            6.893966,53.313119,0.000000
            6.924732,53.311548,0.000000
            6.929655,53.312392,0.000000
            6.934810,53.315353,0.000000
            6.941796,53.314914,0.000000
          </coordinates>
        </LinearRing>
      </outerBoundaryIs>
    </Polygon>
    <Polygon>
      <outerBoundaryIs>
        <LinearRing>
          <tessellate>1</tessellate>
          <coordinates>
            6.905549,53.283453,0.000000
            6.908790,53.282516,0.000000
            6.912146,53.283305,0.000000
            6.916480,53.287575,0.000000
            6.916764,53.288072,0.000000
            6.915251,53.288369,0.000000
            6.915097,53.290097,0.000000
            6.912526,53.292361,0.000000
            6.908052,53.290971,0.000000
            6.905569,53.288875,0.000000
            6.905549,53.283453,0.000000
          </coordinates>
        </LinearRing>
      </outerBoundaryIs>
    </Polygon>
</Placemark>
</Document>
</kml>

BU00100107 Verspreide huizen Eemskanaal(十个月)
布拉!
#样式1
1.
6.941796,53.314914,0.000000
6.942705,53.310923,0.000000
6.952713,53.305394,0.000000
6.954853,53.300262,0.000000
6.954239,53.296317,0.000000
6.962271,53.295483,0.000000
6.995900,53.287338,0.000000
6.995013,53.285264,0.000000
6.996842,53.281429,0.000000
6.991748,53.278255,0.000000
6.990729,53.275234,0.000000
6.988361,53.274477,0.000000
6.990120,53.271780,0.000000
6.984540,53.272709,0.000000
6.984543,53.274393,0.000000
6.980317,53.274404,0.000000
6.975829,53.272503,0.000000
6.974816,53.271125,0.000000
6.963342,53.271937,0.000000
6.955082,53.265909,0.000000
6.945183,53.269634,0.000000
6.940684,53.273351,0.000000
6.935942,53.273875,0.000000
6.934392,53.276351,0.000000
6.929104,53.272181,0.000000
6.909544,53.265952,0.000000
6.908803,53.269015,0.000000
6.909151,53.278897,0.000000
6.888166,53.279161,0.000000
6.887788,53.279639,0.000000
6.886750,53.280950,0.000000
6.886729,53.280977,0.000000
6.888260,53.281856,0.000000
6.895912,53.286254,0.000000
6.892976,53.288089,0.000000
6.891571,53.290803,0.000000
6.887323,53.298046,0.000000
6.887729,53.309725,0.000000
6.887583,53.309816,0.000000
6.888683,53.311891,0.000000
6.893966,53.313119,0.000000
6.924732,53.311548,0.000000
6.929655,53.312392,0.000000
6.934810,53.315353,0.000000
6.941796,53.314914,0.000000
1.
6.905549,53.283453,0.000000
6.908790,53.282516,0.000000
6.912146,53.283305,0.000000
6.916480,53.287575,0.000000
6.916764,53.288072,0.000000
6.915251,53.288369,0.000000
6.915097,53.290097,0.000000
6.912526,53.292361,0.000000
6.908052,53.290971,0.000000
6.905569,53.288875,0.000000
6.905549,53.283453,0.000000

也欢迎使用任何其他解决方案

我不熟悉Jak,但是如果您使用,名称空间就不同了。你有

http://earth.google.com/kml/2.2
OGC名称空间是

http://www.opengis.net/kml/2.2
谷歌使用


还有。您使用的命名空间是在KML被授予OGC作为开放标准之前使用的。

< P>命名空间是不正确的,但是如果您有1700个文件,这是唯一的问题,您可以简单地使用KML的两个参数形式:unMaSHALL(文件文件,布尔验证)。 我还使用了以下俗气的perl脚本来更正我的文件

$ cat ~/bin/fixxmlns
#!/usr/bin/perl

for (@ARGV) {

    open (FH,"<$_");
    open (NFH,">$_.x");

    $look = 1;
    while ($r = <FH>) {
        if ($look && $r =~ /earth.google.com/) {
            $r = qq{<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2">\n};
            $look = 0;
            }
        print NFH $r;
    }
    close (NFH);
    close (FH);
    rename("$_", "$_.orig");
    rename("$_.x", "$_");
}
$ fixxmlns *.kml
$ find parentdir -name "*.kml" -print0 | xargs -0 fixxmlns
$cat~/bin/fixxmlns
#!/usr/bin/perl
for(@ARGV){
开放式(FH,“$x”);
$look=1;
而($r=){
如果($look&&$r=~/earth.google.com/){
$r=qq{\n};
$look=0;
}
打印NFH$r;
}
关闭(NFH);
关闭(FH);
重命名(“$”,“$”;
重命名(“$ux.x”和“$x”);
}
$fixxmlns*.kml
$find parentdir-name“*.kml”-print0 | xargs-0 fixxmlns

这是我的捷径:)


我知道这是错误的,但我没有制作这些文件,我有超过1700个。。。你用什么来代替jak?我其实不是一个Java爱好者,但你可以使用Java XML库来预处理XML文档,检查名称空间,必要时进行更改,然后再交给jak。我看了jaxb,它看起来不错,但一开始就很复杂,因为我的学校作业没有那么长。是的,Jaxb是重量级的。这里有两个选项:只需使用DOM解析,如下所示:。或者,更简单的方法是,以文本形式读入文件,然后使用正则表达式查找并将其更改为正确的名称空间。然后保存它并使用Jak重新处理它
final Kml kml = loadXMLFile("../data/Eemskanaal.kml");
private static Kml loadXMLFile(String path) {
        Kml kml = null;
        try {
            kml = Kml.unmarshal(path);
        } catch (RuntimeException ex) {
            kml = Kml.unmarshal(new File(path), false);
        }
        return kml;
    }
$ cat ~/bin/fixxmlns
#!/usr/bin/perl

for (@ARGV) {

    open (FH,"<$_");
    open (NFH,">$_.x");

    $look = 1;
    while ($r = <FH>) {
        if ($look && $r =~ /earth.google.com/) {
            $r = qq{<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2">\n};
            $look = 0;
            }
        print NFH $r;
    }
    close (NFH);
    close (FH);
    rename("$_", "$_.orig");
    rename("$_.x", "$_");
}
$ fixxmlns *.kml
$ find parentdir -name "*.kml" -print0 | xargs -0 fixxmlns
public static Kml getKml(InputStream is) throws Exception {
    String str = IOUtils.toString( is );
    IOUtils.closeQuietly( is );
    str = StringUtils.replace( str, "xmlns=\"http://earth.google.com/kml/2.2\"", "xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\"" );
    ByteArrayInputStream bais = new ByteArrayInputStream( str.getBytes( "UTF-8" ) );
    return Kml.unmarshal(bais);
}