Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sun XML DOM DocumentBuilder Obj fails when called by a Servlet

 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is strange. If I call this object from a POJO (using main) it works fine. But when I call the same object from a servlet it fails to get the Document object. I am using Tomcat 5.5. Weird. Any ideas?



import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class BuildXMLDoc
{

protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
protected DocumentBuilder builder;
protected Document doc;


public Document getDoc()
{
return doc;
}

public void buldXML(String htmlItem, String htmlType, List dataList)throws Exception {
builder = factory.newDocumentBuilder();
doc = builder.newDocument(); //Fails here. Will not get a ref to the doc obj. Does not throw an exception

Element root = doc.createElement("Object");
doc.appendChild(root);

Node itemInfo = doc.createElement("itemInformation");
root.appendChild(itemInfo);

Node item = doc.createElement("item");
itemInfo.appendChild(item);

Node item2 = doc.createElement("field");
item2.appendChild(doc.createTextNode(htmlItem));
item.appendChild(item2);

item = doc.createElement("item");
itemInfo.appendChild(item);

item2 = doc.createElement("field");
item2.appendChild(doc.createTextNode(htmlType));
item.appendChild(item2);

Node arrayInfo = doc.createElement("arrayList");
root.appendChild(arrayInfo);

Iterator ix = dataList.iterator();
while(ix != null && ix.hasNext()){
String idata = (String) ix.next();
Node dataItem = doc.createElement("item");
arrayInfo.appendChild(dataItem);

Node data = doc.createElement("field");
data.appendChild(doc.createTextNode(idata));
dataItem.appendChild(data);
}

}
}
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could we have some tiny hint about the nature of the failure? All we know so far is that a certain line of code doesn't throw an exception.
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, its puzzling. At this line...

doc = builder.newDocument()

I am suppose to get a reference to a Document object. Instead, I get a null back. But only when I am calling it though Tomcat. If I run it from a Java bean using main(), I get the expected result.

Both Tomcat and the Java Bean are running the same JDK, so the Sun XML class libs, in rt.jar, are availible to both.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you know you get a null back because...
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the debugger I can see that doc = null after the call
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you aren't getting a NullPointerException, then.

All that means is that the toString() method of your Document object returns null. And it does that because the nodeValue() method of a Document also returns null, I suppose. It's entitled to return anything it likes (DOM doesn't specify it) so nodeValue() is as good as anything else.

You are probably used to some other DOM implementation that returns a formatted XML string as the result of toString(). But as I said, the result of toString() in DOM implementations is undefined so you should not rely on it. You are just using different DOM implementations in the two different places.
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the thing is, both implementations are in the same project (In different packages of course) but they both use the same JDK and BuildXMLDoc object. But apparently, Tomcat has its own ideas on how to implement the Sun DOM.
 
Prabodh Reddy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check wheter object doc is an instance of which class
//doc = builder.newDocument();
//if (doc instanceof <<class>>
// print hascode for an object
i think doc is using some other api which is not declared in the import statements in ur program
i think Tomcat will have its own Parsing API for XML.
 
Roopa Prabhu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

did u find the reason why the newDocument() method is returning null?
even i'm getting this problem....

if u get the solution plz write the solution in this forum...

Thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If the second line - which calls a method in doc - does not throw a NPE, then you do in fact have a Document object.
If code such as:
System.out.println("document is " + doc );
says document is null, that may be due to the result of calling toString() on a Document object is indeed null. See Paul Clapham's comment.
There is a great table in the JavaDocs for the org.w3c.dom.Node class that you should get familiar with. It shows that a Node which is a Document object is supposed to return null when getNodeValue() is called.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic