• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to build xml document from Database

Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 com.ibm.xml.parsers.DOMParser;
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;
import java.io.*;
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 **

Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . 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.
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!