How can i grab the xml from this website from within my java app?
http://ws.geonames.org/search?q=hollywood&maxRows=1
Printable View
How can i grab the xml from this website from within my java app?
http://ws.geonames.org/search?q=hollywood&maxRows=1
To read it as text you can use:Code:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
URLConnection conn = java.net.URI.create("http://ws.geonames.org/search?q=hollywood&maxRows=1").toURL().openConnection();
while (!conn.getDoInput());
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (reader.ready()) {
System.out.println(reader.readLine());
}
} catch (MalformedURLException ex) {
Logger.getLogger("global").log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger("global").log(Level.SEVERE, null, ex);
}
}
}
Or this, to get XML itself:Code:import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
java.net.URLConnection conn = java.net.URI.create("http://ws.geonames.org/search?q=hollywood&maxRows=1").toURL().openConnection();
while (!conn.getDoInput());
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream())));
} catch (XMLStreamException ex) {
Logger.getLogger("global").log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger("global").log(Level.SEVERE, null, ex);
}
}
}
That worked great, thanks man!
hmmmmmmmmmm, it seems to freeze every once in a while, any theories on why? How can I make it stop when about 5-6 seconds has passed by? It shouldnt take more than 3 seconds to retrieve the xml. Thanks for your help