aspose file tools*
The moose likes Servlets and the fly likes Sun XML DOM DocumentBuilder Obj fails when called by a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sun XML DOM DocumentBuilder Obj fails when called by a Servlet" Watch "Sun XML DOM DocumentBuilder Obj fails when called by a Servlet" New topic
Author

Sun XML DOM DocumentBuilder Obj fails when called by a Servlet

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Jun 25, 2004
Posts: 388
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
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

And you know you get a null back because...
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
In the debugger I can see that doc = null after the call
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Jun 25, 2004
Posts: 388
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

Joined: Dec 19, 2005
Posts: 14
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

Joined: Mar 16, 2006
Posts: 1
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.


RIP
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sun XML DOM DocumentBuilder Obj fails when called by a Servlet