So I\'m sitting here with Google Geocoder, which returns an XML via \'GOOGLE_URL/xml?address=input&sensor=false\'. I need to fetch it by using Java and parse it into a J
Here is a working example which shows how to connect to a URL, download XML and convert it to JSON format:
Connect to a URL and download the XML as a string:
String str = "http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
URL url = new URL(str);
InputStream is = url.openStream();
int ptr = 0;
StringBuilder builder = new StringBuilder();
while ((ptr = is.read()) != -1) {
builder.append((char) ptr);
}
String xml = builder.toString();
Download the JSON library from here. (You will have to compile it and ensure that the classes are on your classpath.)
Convert the XML into a JSON Object:
JSONObject jsonObject = XML.toJSONObject(xml);
System.out.println(jsonObject);
Why don't you retrieve the Google geocode as JSON in the first place?
The above link is taken directly from: