The moose likes XML and Related Technologies and the fly likes How to build xml document from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to build xml document from Database" Watch "How to build xml document from Database" New topic

How to build xml document from Database


Joined: May 13, 2001
Posts: 15
Please help me in creating xml document
I am new to xml and started recently
First let me put my task here.
I have to create xml document retrieving data from the database.
Now I am not clear whether to go for DOM/SAX.

here I am pasting my simple code sample which I worked out
please suggest me that Can I proceed simalarly with the
Thanking u in advance
** begin code **
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class createXML
public Document getXML()
Document doc=null;
DOMParser domparsersource2 = new DOMParser();
Document Doc1 = domparsersource2.getDocument();

doc = new DocumentImpl();
Node root = doc.createElement("Book");
Node node1 = doc.createElement("Name");
Node node2 = doc.createElement("Price");
Node node3 = doc.createElement("ISBN");
Node textNode1 = doc.createTextNode("Professional XML");
Node textNode2 = doc.createTextNode("500");
Node textNode3 = doc.createTextNode("b-x234");
FileOutputStream fos = new FileOutputStream("test.xml");
OutputFormat of = new OutputFormat();
XMLSerializer slz=new XMLSerializer(fos, of);
catch (Exception e)
return doc;
/*public static void getNodes(Node node)
if (node.hasChildNodes()){
NodeList nl=node.getChildNodes();
System.out.println(node.getNodeName()+" "+node.getNodeValue());
for(int i=0;i<nl.getLength();i++)
public static void main(String args[])
Document D = new createXML().getXML();
Element e =D.getDocumentElement();
createXML createxml=new createXML();
// createxml.getNodes(e);
** end code **

Intelligent Hard work never goes waste
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
If your requirement is to create an XML document, you dont' need either DOM or SAX since they are parsing tools.
All XML documents are pure ASCII based text files. Static XML documents can be created using any text utility like notepad/wordpad etc. Dynamic XML documents can be built "on the fly" by your Java program just the way you create any other text file and write information to it. Simply create a FileReader, and start writing XML chunks into it. When you're done, just close it You can then verify the validity of your document( for testing purposes only ) by opening it in a web browser like IE or Netscape that has built in XML support.
Good luck,

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: How to build xml document from Database
It's not a secret anymore!