Results 1 to 5 of 5

Thread: Grab XML and work with it

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  4. #4

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Grab XML and work with it

    That worked great, thanks man!

  5. #5

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    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
  •  



Click Here to Expand Forum to Full Width