Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:22:21+00:00 2026-06-17T08:22:21+00:00

Specifically, I was using dom4j to read in KML documents and parsing out some

  • 0

Specifically, I was using dom4j to read in KML documents and parsing out some of the data in the XML. When I just pass in the URL in string form to the reader, it’s so simple and handles both file system URLs and web URLs:

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

The problem is, sometimes my code will need to handle KMZ documents, which are basically just zipped up XML (KML) documents. Unfortunately, there’s no convenient way to handle this with the SAXReader. I’ve found all kinds of funky solutions to determining if any given file is a ZIP file, but my code quickly becomes blown up and nasty — reading the stream, building a file, checking the “magic” hex bytes at the beginning, extracting, etc.

Is there some quick and clean way to handle this? An easier way to connect to any URL and extract the contents if they’re compressed, otherwise simply grab the XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:22:22+00:00Added an answer on June 17, 2026 at 8:22 am

    Hmm, it doesn’t seem the KMZDOMLoader handles kmz files on the web. It’s possible that the kmz is being loaded dynamically so it won’t always have a) a file reference or b) a .kmz extension specifically — it’ll have to determine by content type.

    What I ended up doing was to build a URL object, then get the protocol. I have separate logic to handle a local file or a document on the web. Then inside each of those logic blocks, I had to determine if it was compressed. The SAXReader read() method takes an input stream, so I found that I could use a ZipInputStream for the kmzs.

    Here’s the code I ended up with:

    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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm specifically using C# with Ninject but the problem stretches beyond just Ninject. My
I am parsing a CSV file using fgetcsv, specifically using $line_of_text. I want to
I'm having trouble debugging a multi-process application (specifically using a process pool in python's
I'm using Mercurial (specifically TortoiseHg on Windows) to do version control of VBA code.
I'm working on an application using MEF (specifically, MEF 2 Preview 5 ), and
I'd like to implement MVC while using LINQ (specifically, LINQ-to-entities). The way I would
Does anyone know how to modify the thread scheduling (specifically affinity) when using TBB?
I'm attempting to build a ASP.NET website using MSBuild - specifically the AspNetCompiler tag.
I am writing an app that uses the TimeSpan object. Specifically I am using
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.