How to parse a kml/xml file in Java

Posted on 2009-04-28
Last Modified: 2013-11-23

I'm trying to geocode an address with google maps.

You can look at the kml file I am trying to parse by typing:,Munich,Germany&output=xml&key=abcdefg

into your browser.

I try to create a DOM document from the kml file returned by the google maps server in Java, but I am  encountering problems when there are german "Umlaute (special characters like 'ä,'ö','ü')" in the kml response. Is there any way to parse the kml file differently, so I don't get an exception thrown?

Thanks for any help!

String question = ",Munich,Germany&output=xml&key=abcdefg"

try {

            builder = builderFactory.newDocumentBuilder();

        } catch (ParserConfigurationException e) {



        try {

// Following line throws an exception of type:

// Invalid byte 2 of 2-byte UTF-8 sequence.


            Document document = builder.parse(question);

        } catch (SAXException e) {


Open in new window

Question by:Waschtel
    1 Comment

    Accepted Solution

    I found a solution, although it is a bit verbose, and I don' t really understand why it works...

    Maybe there's something shorter out there.

    //Create a URL pointing to the Goole Maps gateway.
    URL url = new URL(URLencoder.encode(",Munich,Germany&output=xml&key=abcdefg"
    InputStream xmlStream = url.openStream()"),"UTF-8");
    //Create a BuilderFactory
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    //Create a Builder
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    // Get the Stream
    InputStream xmlStream = url.openStream();
    //Measure the Stream and create a buffer
    int size = xmlStream.available();
    byte[] xmlBuffer=new byte[size];
    //Fill the buffer with the Stream;
    //Create a String from the Buffer using UTF-8 decoding
    String xmlString = new String(xmlBuffer,Charset.forName("UTF-8"));
    //Create a ByteArrayInputStream from the String
                ByteArrayInputStream xmlMassaged = new ByteArrayInputStream(xmlString.getBytes());
    //Create a Document from this ByteArrayInputStream;
                document = builder.parse(xmlMassaged);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    sumDigits  challenge 7 38
    Books that can get me started on JAVA 2 33
    endX challenge 2 30
    count11 challenge 6 31
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now