Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: School Java Project Help (XML and links)

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Solved]School Java Project Help (XML and links)

    Hello everyone.

    I'm pretty much a pure newbie when it comes to Java and OO programming in general. Anyway I have to take a Java class as part of my major and the project is literally stumping me.

    Firstly, the prototype of my program is due before he even teaches us some of the material we need to do it. Hence my following questions. Sorry in advance if I started this thread in the wrong area.

    The project is where we make a program to take a user's input in Town, State form and get the weather for that town. He wants us to use XML documents from NOAA.gov. I know I have to convert the Town, State format into a Latitude, Long type format to plug into the URL to access the XML file at NOAA.

    So firstly... I'm gonna geocode the address. Google geocoding API requires you to send a request via URL to retrieve an XML document. How could I make my Java program execute a URL?

    Secondly how would I access the data off that XML file it returns.

    Like if I sent a request for my hometown of Apollo, PA to the Google geocoding service and it returns an XML document with tons of data... how do I pull that data for use in my program?

    Sorry if I sound like I have no idea what I'm talking about. I don't even require in-depth answers, just get me going in the right direction.

    Thanks guys!
    Last edited by MC2170; March 16th, 2011 at 03:52 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: School Java Project Help (XML and links)

    Hello MC2170, welcome to the Java Programming Forums.

    You can find lots of code examples here - Java Code Snippets and Tutorials

    You can download the XML file by using something like this - http://www.javaprogrammingforums.com...bsite-url.html

    You would need to write the extracted data to a file.

    To parse the XML data, you could use - http://www.javaprogrammingforums.com...dom-parse.html

    Make a start on this application. Show us what you have done and pinpoint where you are stuck.
    We can take it from there..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Java Project Help (XML and links)

    Thank you a ton for the linkage. I'm in a bit of a stressful situation regarding this project.
    I'm excited to learn Java, but just OO in general is confusing. I keep trying to write this program as though its C++ or something crazy like that. Honestly... just to make two classes interact is a bit deal to me. I'll post what I have soon.
    Last edited by MC2170; March 14th, 2011 at 01:56 PM.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: School Java Project Help (XML and links)

    Practise makes perfect One of those code examples will give you a good base to start off with.
    Like I say, show us the code you have written and when you get stuck, we can help you move forward.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Java Project Help (XML and links)

    What I currently need to do is somehow pull the latitude or lat from the XML document and store it as a double variable. The XML document is a result of executing this "http://maps.googleapis.com/maps/api/geocode/xml?address=apollo,+pa&sensor=true".

    I honestly have no idea how to do so.
    Its embarrassing to actually post my code because I am some helplessly lost at this point.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Java Project Help (XML and links)

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    import java.io.*;
     
    public class XMLReader {
     
    	public static void main(String[] args) {
     
    		//File file = new File("F:\\apollo.xml");
    		File file = new File("http://maps.googleapis.com/maps/api/geocode/xml?address=apollo,+pa&sensor=true");
     
    		try {
    			  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    			  Document doc = builder.parse(file);
     
    			  NodeList nodes = doc.getElementsByTagName("location");
    			  Element element = (Element) nodes.item(0);
     
    			  NodeList lat = element.getElementsByTagName("lat");
    			  Element latent = (Element) lat.item(0);
     
    			  NodeList lng = element.getElementsByTagName("lng");
    			  Element lngent = (Element) lng.item(0);
     
    			  System.out.println("Latitude: " + latent.getFirstChild().getTextContent());
    			  System.out.println("Longitude: " + lngent.getFirstChild().getTextContent());
     
    		}
     
    		catch (Exception e) {
    	    e.printStackTrace();
     
    		}
    	}
    }

    Sorry for the double post.
    I can get the silly thing to get the lat and lng from the XML if it is saved.
    As you can see I was pretending that maybe I could pull it from the URL which doesn't work as I assumed.
    There has to be some way to do it.

    Edit: And I figured it out.

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    //import java.io.*;
     
    public class XMLReader {
     
    	public static void main(String[] args){
     
    		try {
     
    			  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    			  Document doc = builder.parse("http://maps.googleapis.com/maps/api/geocode/xml?address=apollo,+pa&sensor=true");
     
    			  NodeList nodes = doc.getElementsByTagName("location");
    			  Element element = (Element) nodes.item(0);
     
    			  NodeList lat = element.getElementsByTagName("lat");
    			  Element latent = (Element) lat.item(0);
     
    			  NodeList lng = element.getElementsByTagName("lng");
    			  Element lngent = (Element) lng.item(0);
     
    			  System.out.println("Latitude: " + latent.getFirstChild().getTextContent());
    			  System.out.println("Longitude: " + lngent.getFirstChild().getTextContent());
     
    		}
     
    		catch (Exception e) {
    	    e.printStackTrace();
     
    		}
    	}
    }

    Now I just gotta plug it into my main program.
    Those links you posted helped a ton. Thanks
    Last edited by MC2170; March 16th, 2011 at 03:51 PM.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Java Project Help (XML and links)

    There are two ways you can relate two classes. Using a "Is A" relation and "Has A" relation. If you want to understand it in detail, following is an article i have written
    Object Oriented Programming Blog, Tutorial,Java, Training,

Similar Threads

  1. Java: Links, images, font and divisions with HTML / CSS
    By Cyloc in forum AWT / Java Swing
    Replies: 3
    Last Post: August 2nd, 2011, 12:54 PM
  2. School java project, completely stuck
    By John1818 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 18th, 2010, 03:10 AM
  3. school project
    By robin28 in forum Java Theory & Questions
    Replies: 13
    Last Post: November 12th, 2010, 08:11 AM
  4. School Help
    By JavaNewb in forum Loops & Control Statements
    Replies: 1
    Last Post: March 24th, 2010, 09:31 PM