Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to build xml document from Database

 
Pardha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 **

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Ajith Kallambella
Sheriff
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,

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic