2012-02-08 4 views
3

내가 기자 협회와 MKL 파일을 가져올려고와 KML 그러나 나는 다음과 같은 오류 얻을 :수입 자바

javax.xml.bind.UnmarshalException : (예상치 못한 요소를 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()); 
     } 

그리고 이것은 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> 

는 다른 솔루션도 환영

답변

0

네임 스페이스가 올바르지 않지만 1700 개의 파일 만있는 경우이 두 가지 인수 형식의 Kml.unmarshal (파일 file, 부울 validate)을 사용하는 것이 좋습니다.

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

다음 cheezy 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 
1

내가 기자 협회에 익숙하지 않은,하지만 당신이 ' OGC Schema을 사용하면 네임 스페이스가 다릅니다. 당신은

http://earth.google.com/kml/2.2 

을 가지고있는 OGC 네임 스페이스는 구글 extension schema뿐만 아니라

http://www.google.com/kml/ext/2.2 

를 사용

http://www.opengis.net/kml/2.2 

입니다. 사용중인 네임 스페이스는 KML이 공개 표준으로 OGC에 주어지기 전에 Google에서 사용했습니다.

+0

입니다 재크 대신에? – clankill3r

+0

저는 실제로 자바 녀석이 아니지만 Java XML 라이브러리를 사용하여 XML 문서를 사전 처리하고 네임 스페이스를 확인한 다음 Jak으로 넘겨주기 전에 필요에 따라 바꿀 수 있습니다. –

+0

jaxb는 좋게 보였지만 학교 과제물이 그리 길지 않아 시작하기가 상당히 복잡합니다. – clankill3r

2

여기에 나는 그것이 잘못하지만 난 파일을하지 않았고 내가 당신은 무엇을 사용합니까 ... 그들 더 후 1,700 알고 내 신속하고 더러운 방법 :

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); 
} 
+0

좋은 영리한 솔루션을 사용하여 다시 처리 – clankill3r