2013-01-15 4 views
0

특히 dom4j를 사용하여 KML 문서를 읽고 XML의 일부 데이터를 파싱했습니다. 난 그냥 독자에게 문자열 형태로 URL을 전달하면 너무 간단하고 파일 시스템 URL과 웹 URL을 모두 처리합니다dom4j로 압축 된 XML 문서 처리

SAXReader reader = new SAXReader(); 
Document document = reader.read(url); 

문제는, 가끔 내 코드는 KMZ 문서입니다 처리 할 필요가있다 기본적으로 XML (KML) 문서를 압축합니다. 불행하게도, SAXReader로 이것을 처리 할 편리한 방법은 없습니다. 주어진 파일이 ZIP 파일인지 판단하는 데 모든 종류의 펑키 솔루션을 찾았지만 코드가 빠르게 날아가고 불쾌 해졌습니다. 스트림 읽기, 파일 작성, 처음에는 "매직"16 진수 바이트 확인, 추출 등.

처리 할 수있는 빠르고 쉬운 방법이 있습니까? URL에 연결하고 압축 된 경우 내용을 추출하는 더 쉬운 방법입니다. 그렇지 않으면 간단히 XML을 잡으시겠습니까?

답변

0

흠, KMZDOMLoader가 웹상의 kmz 파일을 처리하지 않는 것처럼 보입니다. kmz가 동적으로로드되어 항상 파일 참조 또는 b) .kmz 확장자를 가질 필요는 없습니다. 콘텐츠 유형별로 결정해야합니다.

내가 끝낸 것은 URL 개체를 작성한 다음 프로토콜을 얻는 것입니다. 웹상의 로컬 파일이나 문서를 처리하기위한 논리가 따로 있습니다. 그런 다음 각 논리 블록 내부에서 압축되었는지 확인해야했습니다. SAXReader read() 메서드는 입력 스트림을 사용하므로 kmz에 대해 ZipInputStream을 사용할 수 있음을 알았습니다.

private static final long ZIP_MAGIC_NUMBERS = 0x504B0304; 
private static final String KMZ_CONTENT_TYPE = "application/vnd.google-earth.kmz"; 

private Document getDocument(String urlString) throws IOException, DocumentException, URISyntaxException { 
     InputStream inputStream = null; 
     URL url = new URL(urlString); 
     String protocol = url.getProtocol(); 

     /* 
     * Figure out how to get the XML from the URL -- there are 4 possibilities: 
     * 
     * 1) a KML (uncompressed) doc on the filesystem 
     * 2) a KMZ (compressed) doc on the filesystem 
     * 3) a KML (uncompressed) doc on the web 
     * 4) a KMZ (compressed) doc on the web 
     */ 
     if (protocol.equalsIgnoreCase("file")) { 
      // the provided input URL points to a file on a file system 
      File file = new File(url.toURI()); 
      RandomAccessFile raf = new RandomAccessFile(file, "r"); 
      long n = raf.readInt(); 
      raf.close(); 

      if (n == KmlMetadataExtractorAdaptor.ZIP_MAGIC_NUMBERS) { 
       // the file is a KMZ file 
       inputStream = new ZipInputStream(new FileInputStream(file)); 
       ((ZipInputStream) inputStream).getNextEntry(); 
      } else { 
       // the file is a KML file 
       inputStream = new FileInputStream(file); 
      } 

     } else if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) { 
      // the provided input URL points to a web location 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      String contentType = connection.getContentType(); 

      if (contentType.contains(KmlMetadataExtractorAdaptor.KMZ_CONTENT_TYPE)) { 
       // the target resource is KMZ 
       inputStream = new ZipInputStream(connection.getInputStream()); 
       ((ZipInputStream) inputStream).getNextEntry(); 
      } else { 
       // the target resource is KML 
       inputStream = connection.getInputStream(); 
      } 

     } 

     Document document = new SAXReader().read(inputStream); 
     inputStream.close(); 

     return document; 
    } 
:

여기에 내가 함께 종료 된 코드는