|
-
Sep 6th, 2007, 10:17 PM
#1
Thread Starter
Hyperactive Member
Grab XML and work with it
How can i grab the xml from this website from within my java app?
http://ws.geonames.org/search?q=hollywood&maxRows=1
-
Sep 8th, 2007, 02:54 PM
#2
Re: Grab XML and work with it
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);
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 8th, 2007, 03:06 PM
#3
Re: Grab XML and work with it
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);
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 21st, 2007, 01:26 PM
#4
Thread Starter
Hyperactive Member
Re: Grab XML and work with it
That worked great, thanks man!
-
Sep 24th, 2007, 01:55 PM
#5
Thread Starter
Hyperactive Member
Re: Grab XML and work with it
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|