[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to parse a kml/xml file in Java

Posted on 2009-04-28
1
Medium Priority
?
2,290 Views
Last Modified: 2013-11-23
Hi,

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

You can look at the kml file I am trying to parse by typing:

http://maps.google.com/maps/geo?q=Hofbrunnstrasse+39,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!

Sebastian.
String question = "http://maps.google.com/maps/geo?q=Hofbrunnstrasse+39,Munich,Germany&output=xml&key=abcdefg"
try {
            builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        try {
// Following line throws an exception of type:
//com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.
 
            Document document = builder.parse(question);
        } catch (SAXException e) {
            e.printStackTrace();

Open in new window

0
Comment
Question by:Waschtel
1 Comment
 

Accepted Solution

by:
Waschtel earned 0 total points
ID: 24261531
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("http://maps.google.com/maps/geo?q=Hofbrunnstrasse+39,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
xmlStream.read(xmlBuffer);
 
//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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 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