documentBulder.parse(string) returns [#document: null]

A

amitatgroups

import java.io.*;
import java.net.*;

import org.w3c.dom.*;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.InputSource;
import org.w3c.dom.Text;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


public class GetWeather{
public static void main(String []arg){
String path = "http://xml.weather.yahoo.com/forecastrss?
p=INXX0038&u=f";
try{
URL url = new URL(path);
URLConnection urlcon = url.openConnection();

//int responseCode =urlcon.getResponseCode ( ) ;
//if ( responseCode ==HttpURLConnection.HTTP_OK) {}

BufferedReader in = new BufferedReader(new
InputStreamReader(urlcon.getInputStream()));
String inputLine = null;
String xmlString = "";
while ((inputLine = in.readLine()) != null){
xmlString = xmlString+inputLine;
}
//System.out.println(xmlString);
try{
parseXml(xmlString);
//parseXml(urlcon.getInputStream());
}catch(Exception ex){
System.out.println("exception in parseXml():-> "+ex);
}

}catch(Exception ex){
System.out.println("exception:-> "+ex);
}

}

public static void parseXml(String xml) throws Exception{
//public static void parseXml(InputStream xml) throws Exception{
Document doc;
DocumentBuilder docBuilder;
DocumentBuilderFactory docFactory;

docFactory = DocumentBuilderFactory.newInstance();

docBuilder = docFactory.newDocumentBuilder();

doc = docBuilder.parse(new InputSource(new StringReader(xml))); //**
Line abc **/
//doc = docBuilder.parse(xml);
System.out.println("doc:-> "+doc); //** Line pqr **/
Element parentEle = doc.getDocumentElement();

NodeList nodeList = parentEle.getChildNodes();
NamedNodeMap nodeAttr = null;
for(int i=0;i<nodeList.getLength();i++){
nodeAttr = nodeList.item(i).getAttributes();
for(int j=0;j<nodeAttr.getLength();j++){
System.out.println("Node Attr :-
"+nodeAttr.item(j).getNodeValue());
System.out.println("----------------");
}
}
}
}


/**Line abc*/ returns null

doc:-> [#document: null]
exception in parseXml():-> java.lang.NullPointerException

why ?
how can i resolve exception
 
D

Daniel Pitts

import java.io.*;
import java.net.*;

import org.w3c.dom.*;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.InputSource;
import org.w3c.dom.Text;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class GetWeather{
public static void main(String []arg){
String path = "http://xml.weather.yahoo.com/forecastrss?
p=INXX0038&u=f";
try{
URL url = new URL(path);
URLConnection urlcon = url.openConnection();

//int responseCode =urlcon.getResponseCode ( ) ;
//if ( responseCode ==HttpURLConnection.HTTP_OK) {}

BufferedReader in = new BufferedReader(new
InputStreamReader(urlcon.getInputStream()));
String inputLine = null;
String xmlString = "";
while ((inputLine = in.readLine()) != null){
xmlString = xmlString+inputLine;
}
//System.out.println(xmlString);
try{
parseXml(xmlString);
//parseXml(urlcon.getInputStream());
}catch(Exception ex){
System.out.println("exception in parseXml():-> "+ex);
}

}catch(Exception ex){
System.out.println("exception:-> "+ex);
}

}

public static void parseXml(String xml) throws Exception{
//public static void parseXml(InputStream xml) throws Exception{
Document doc;
DocumentBuilder docBuilder;
DocumentBuilderFactory docFactory;

docFactory = DocumentBuilderFactory.newInstance();

docBuilder = docFactory.newDocumentBuilder();

doc = docBuilder.parse(new InputSource(new StringReader(xml))); //**
Line abc **/
//doc = docBuilder.parse(xml);
System.out.println("doc:-> "+doc); //** Line pqr **/
Element parentEle = doc.getDocumentElement();

NodeList nodeList = parentEle.getChildNodes();
NamedNodeMap nodeAttr = null;
for(int i=0;i<nodeList.getLength();i++){
nodeAttr = nodeList.item(i).getAttributes();
for(int j=0;j<nodeAttr.getLength();j++){
System.out.println("Node Attr :->"+nodeAttr.item(j).getNodeValue());

System.out.println("----------------");
}
}
}

}

/**Line abc*/ returns null

doc:-> [#document: null]
exception in parseXml():-> java.lang.NullPointerException

why ?
how can i resolve exception

have you tried this:

Document document =
DocumentBuilderFactory.newInstance().newBuilder().parse("http://
xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f");

That way, you don't have to mess with URLs or URLConnnections at all.
 
A

Andrew Thompson

I try what you suggest me but still i found same result.

I tried your original code and got the same confusing
result you got. The data of the XML is definitely in the
string, it can seen when printed out. If you take the
string and check it for well-formedness, it checks OK
as well. Yet the call to docBuilder.parse not only
throws no exceptions (as I might expect if the XML
were not well-formed) but returns a 'null' Document.

By my understanding of the JavaDocs for the parse
method, it should either return a valid Document object,
or throw parsing related Exceptions (before the NPE
in the code shown).

As another note, try to get in the habit of dumping
the stack trace of the Exception - it contains more
and better information. E.G. ..

}catch (Exception ex){
System.out.println("exception:-> "+ex);
ex.printStackTrace();
}


[ And as an aside, Daniel's short-hand makes a lot of
sense for working code, but for debugging, I think it is
best to give the code as many failure 'points' (on lines
of code) as possible - to help pin down which part
breaks. Stack traces are a lot more useful for the type
of 'step by step' code you first posted, than for the
'short-hand' way of writing the code. ]

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
M

Manfred Rosenboom

I have used the following code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc =
db.parse("http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f");
System.out.println("doc : " + doc);

With the above code I get the following output:


Warning: validation was turned on but an org.xml.sax.ErrorHandler was not
set, which is probably not what is desired. Parser will use a default
ErrorHandler to print the first 10 errors. Please call
the 'setErrorHandler' method to fix this.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=2: Element type "rss" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=3: Element type "channel" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=4: Element type "title" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=5: Element type "link" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=6: Element type "description" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=7: Element type "language" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=8: Element type "lastBuildDate" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=9: Element type "ttl" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=10: Element type "yweather:location" must be declared.
Error: URI=http://xml.weather.yahoo.com/forecastrss?p=INXX0038&u=f
Line=11: Element type "yweather:units" must be declared.
doc : [#document: null]


Best,
Manfred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top