wood burning stoves 2.0 *
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
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
Author

How to build xml document from Database

Pardha
Greenhorn

Joined: May 13, 2001
Posts: 15
Hi
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
database?
Thanking u in advance
Saradhi
** 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;
try
{
DOMParser domparsersource2 = new DOMParser();
Document Doc1 = domparsersource2.getDocument();

doc = new DocumentImpl();
Node root = doc.createElement("Book");
doc.appendChild(root);
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");
node1.appendChild(textNode1);
node2.appendChild(textNode2);
node3.appendChild(textNode3);
root.appendChild(node1);
root.appendChild(node2);
root.appendChild(node3);
FileOutputStream fos = new FileOutputStream("test.xml");
OutputFormat of = new OutputFormat();
XMLSerializer slz=new XMLSerializer(fos, of);
slz.serialize(doc);
}
catch (Exception e)
{
System.err.println(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++)
{
nl.item(i);
getNodes(nl.item(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
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"saradhi74",
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.
Thanks.

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

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).
 
 
subject: How to build xml document from Database