Java and displaying a Document obect

Annorax's Avatar, Join Date: Nov 2006
Newbie Member
I have a method which writes an XML to an object of type Document, as follows:

Document oDoc = new DocumentImpl();

Element rootElement = oDoc.createElement("ROOT_ELEMENT_NAME");
rootElement.setAttribute("ATTRIBUTE_1", "VALUE_OF_ATTRIBUTE");
rootElement.setAttribute("ATTRIBUTE_2", "VALUE_OF_ATTRIBUTE");
rootElement.setAttribute("ATTRIBUTE_3", "VALUE_OF_ATTRIBUTE");
rootElement.setAttribute("ATTRIBUTE_4", "VALUE_OF_ATTRIBUTE");
oDoc.appendChild(rootElement);

Element element_1 = oDoc.createElement("CHILD_ELEMENT_1");
element_1.setAttribute("ATTRIBUTE_1", "VALUE_OF_ATTRIBUTE");
element_1.setAttribute("ATTRIBUTE_2", "VALUE_OF_ATTRIBUTE");
element_1.setAttribute("ATTRIBUTE_3", "VALUE_OF_ATTRIBUTE");
element_1.setAttribute("ATTRIBUTE_4", "VALUE_OF_ATTRIBUTE");
rootElement.appendChild(element_1);

Element element_2 = oDoc.createElement("CHILD_ELEMENT_2");
element_2.setAttribute("ATTRIBUTE_1", "VALUE_OF_ATTRIBUTE");
element_2.setAttribute("ATTRIBUTE_2", "VALUE_OF_ATTRIBUTE");
element_2.setAttribute("ATTRIBUTE_3", "VALUE_OF_ATTRIBUTE");
element_2.setAttribute("ATTRIBUTE_4", "VALUE_OF_ATTRIBUTE");
rootElement.appendChild(element_2);

Element element1_1 = oDoc.createElement("CHILD_ELEMENT_1_1");
element1_1.setAttribute("ATTRIBUTE_1", "VALUE_OF_ATTRIBUTE");
element1_1.setAttribute("ATTRIBUTE_2", "VALUE_OF_ATTRIBUTE");
element1_1.setAttribute("ATTRIBUTE_3", "VALUE_OF_ATTRIBUTE");
element1_1.setAttribute("ATTRIBUTE_4", "VALUE_OF_ATTRIBUTE");
element_1.appendChild(element1_1);

return oDoc;

I want to print the object oDoc to the screen, but every time I do, using:

System.out.println(oDoc);

It returns as this:

[#document: null]

My theory is that there's some method of Document that will display the contents of the object on the screen. Can someone please tell me what that is? Thanks for any help.
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
May be the object is not being created properly, debug with a try-catch block.