dcsimg
No Content in XML File !!!!!
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sudhakar_Santhanam
Posted On:   Saturday, May 11, 2002 10:51 PM

Hi There I have a small problem with xml document creation using Java-XMl Apis. Hope somebody could point out where the error is and solve this. I am creating a DOM Tree using java apis(org.w3c.dom APIs).The problem is when I try to transform the DOM Tree created , to an xml file I cannot find anything in the XML File except the top xml header statement and the comment alone. I have given the part of code which actually creates a DOMTree and transforms the Node created to an xml file using TransformerFactory class. public final void createDOMTree() throws Throwable { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Docume   More>>

Hi There

I have a small problem with xml document creation using Java-XMl Apis.
Hope somebody could point out where the error is and solve this.


I am creating a DOM Tree using java apis(org.w3c.dom APIs).The problem is
when I try to transform the DOM Tree created , to an xml file I cannot
find anything in the XML File except the top xml header statement and the comment alone.
I have given the part of code which actually creates a DOMTree and
transforms the Node created to an xml file using TransformerFactory class.


public final void createDOMTree() throws Throwable

{

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document document = db.newDocument();

System.out.println(document.toString());

Comment comment = document.createComment("A sample XML Document");

document.appendChild(comment);

Element root = document.createElement("top");

System.out.println(root.toString());

Element firstChild = document.createElement("firstchild");

firstChild.setAttribute("debug","false");

root.appendChild(firstChild);

System.out.println(firstChild.toString());

Element secondChild = document.createElement("secondchild");

secondChild.setAttribute("debug","false");

root.appendChild(secondChild);

System.out.println(secondChild.toString());

Element thirdChild = document.createElement("thirdchild");

thirdChild.setAttribute("debug","false");

root.appendChild(thirdChild);

System.out.println(thirdChild.toString());

PrintWriter bw = new PrintWriter(new FileWriter("c:/temp/sample.xml"));

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transformer = tf.newTransformer();

DOMSource domsource = new DOMSource(document);

StreamResult sr = new StreamResult(bw);

transformer.transform(domsource,sr);

NodeList nl = document.getElementsByTagName("top");

for(int i=0;i

{

Node node = nl.item(i);

System.out.println(node.getNodeName());

}

bw.flush();

bw.close();

System.out.println("Successfully created XML File..");

}


The Debug statements prints the following.










But What I needed is this :












Can anybody tell me where the error lies ??.


Thanks in advance

.

   <<Less

Re: No Content in XML File !!!!!

Posted By:   Lunkwill_Vroomfondel  
Posted On:   Sunday, May 12, 2002 02:51 PM

didnt see you doing something like this:


document.appendChild(root);


or


document.setRootElement(root);


...and i think you should do it.
About | Sitemap | Contact