Click to See Complete Forum and Search --> : Grab XML and work with it
xxarmoxx
Sep 6th, 2007, 10:17 PM
How can i grab the xml from this website from within my java app?
http://ws.geonames.org/search?q=hollywood&maxRows=1
ComputerJy
Sep 8th, 2007, 02:54 PM
To read it as text you can use: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);
}
}
}
ComputerJy
Sep 8th, 2007, 03:06 PM
Or this, to get XML itself: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);
}
}
}
xxarmoxx
Sep 21st, 2007, 01:26 PM
That worked great, thanks man!
xxarmoxx
Sep 24th, 2007, 01:55 PM
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.