I have written a program that reads in a File object (really an XML file) to parse the information I want. I have an example program to basically just dump data from the net (program below displaying a webpage). I want to use the code below in accessing the web, to parse that data with my current xml parser with the command line. Like if I did
java xmlParser http://www.engadget.com/rss.xml
Then the program would parse that feed and display just the fields I want in a nice format. First, is that poassible? I’m having trouble just opening up the webpage through the command line instead of what I have below.
Second, how do I take this feed from the net and make my program parse it since it currently just parses a hard-coded XML file. Although not an elegant solution, I could see possibly writing the data from the web into an xml file and parsing that with my XML parser. Any thoughts? Thanks.
Code:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
public class DumpIO {
public static void main(String[] args) throws IOException {
URL url = null;
if (args.length == 0)
url = new URL("http://www.engadget.com/rss.xml");
InputStream is = url.openStream();
Scanner in = new Scanner(is);
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
}
}
Your program works fine if you run it with no command-line args:
The only problem is that you’re only create the
URLifargs.length == 0. If you pass a URL on the command-line thenurlends up beingnull.Fixing that is easy enough:
As for your second question, your XML parser will very likely accept
InputStreams as input. Take theInputStreamyou get fromurl.openStream()and pass it to your XML reader and voilà. No need to save to a temporary file or anything roundabout like that.