[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!
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..
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.
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.
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.
Re: School Java Project Help (XML and links)
Code Java:
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.
Code Java:
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 :D
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,