tags:

views:

35

answers:

2

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());
        }
    }
}
+2  A: 

Your program works fine if you run it with no command-line args:

jkugelman$ java DumpIO
<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
>
<channel>
...

The only problem is that you're only create the URL if args.length == 0. If you pass a URL on the command-line then url ends up being null.

jkugelman$ java DumpIO http://example.com/
Exception in thread "main" java.lang.NullPointerException
    at DumpIO.main(DumpIO.java:11)

Fixing that is easy enough:

if (args.length > 0) {
    url = new URL(args[0]);
}
else {
    url = new URL("http://www.engadget.com/rss.xml");
}

As for your second question, your XML parser will very likely accept InputStreams as input. Take the InputStream you get from url.openStream() and pass it to your XML reader and voilà. No need to save to a temporary file or anything roundabout like that.

John Kugelman
A: 

You could try the same thing in Scala. Here's the entire program:

object DumpIO {
  def main(a: Array[String]) = {
    val url = if (a.size > 0) a(0) else "http://www.engadget.com/rss.xml"
    val rss = xml.XML.load(url)
    println(rss)
  }
}

Scala has excellent native XML support, so you could go on to use the value 'rss' to do xpath like operations, pretty print, etc.

Your Scala program will compile to standard .class files, so you can run it with the java command if necessary.

Synesso