I am trying to use the DOM parser to parser an XML file from the NPR news feed. I need the titles and descriptions of the articles to appear on a GUI (which I haven't written yet, trying to get them to work with just system.out first). When trying to run this I get the error
java.lang.NullPointerException
at NPRDOM.NPRInfo(NPRDOM.java:26)
at NPRDOM.main(NPRDOM.java:9)
I looked up what the null pointer exception was, but I'm not entirely sure what part of this I am writing incorrectly. Any tips or "pointers" ;P would be tremendously appreciated. Thanks a lot!
//import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class NPRDOM {
public static void main(String[] args) throws Exception {
NPRInfo();
}
public static void NPRInfo() throws Exception {
try {
//URL XmlFile = new URL("http://ift.tt/1aw1z0Z");
DocumentBuilderFactory XmlBuilder = DocumentBuilderFactory.newInstance();
DocumentBuilder xBuilder = XmlBuilder.newDocumentBuilder();
Document xml = xBuilder.parse(new URL("http://ift.tt/1aw1z0Z").openStream());
xml.getDocumentElement().normalize();
NodeList nList = xml.getElementsByTagName("channel");
int temp = 0;
for (temp = 0; temp < nList.getLength(); temp++);
Node nNode = nList.item(temp);
System.out.println("Current Element " + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) nNode;
System.out.println("Title : " + eElement.getAttribute("title"));
System.out.println("Description : "+ eElement.getAttribute("description"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment