aspose file tools*
The moose likes XML and Related Technologies and the fly likes Building XML 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 "Building XML" Watch "Building XML" New topic
Author

Building XML

Paula, Nordine
Greenhorn

Joined: May 04, 2001
Posts: 6
Hi all,
Can anybody send me a sample code as to how to construct dynamically XML document from the data coming from the Database.
Apache XERCES would be better.
Thanx in advance
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Are you talking about constructing a DOM in memory or just a text document in XML format? If the latter, just write text like any other text stream.
Bill
Paula, Nordine
Greenhorn

Joined: May 04, 2001
Posts: 6
Hi,
can u send me a sample code please.
I want to create an XML document dynamically with dat coming from database
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Do you have a real project you are working on, or you just want to look at any code example?
If the latter, maybe you can find something here: http://www.xmlpitstop.com/default.asp?DataType=XMLEXAMPLES
If you have a real project, than it becomes more complicated
First, your database may have built-in features for converting tables to XML files, check it. If not, you have to do all the job yourself and you need:
1) design your XML structure;
2) access your database to read data;
3) when you have your data retrieved, as Bill said, either build XML tree or simply write to text file.
Task #1: there are a lot of design considerations involved, �Professional XML databases� chapter1 and chapter2 are entirely dedicated to design stage.
Task #2 is really specific to your database, There is no code that can read �a database�. Task #3 is, on the contrary, generic, any piece of code that construct XML tree will give you an idea. Here is an example.
Did it help?

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Paula, Nordine
Greenhorn

Joined: May 04, 2001
Posts: 6
Hi,
I am hereby attaching the code which I tried it with DOM API using XERCES.Can anybody give me the equivalent code using SAX API
=============================================================
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;
import java.sql.*;

public class buildXML
{
static ResultSet rs = null;
static String emp="";
static Connection Con = null;

public void printDOMTree(Node node)
{
int type = node.getNodeType();
//System.out.println("Type of element is " + type);
//System.out.println("The doc/node inside printDOMTree is " + node);
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
//System.out.println("I'm calling from Node.DocUMENT_NODE");
System.out.println("<?xml version=\"1.0\" ?>");
printDOMTree(((Document)node).getDocumentElement());
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
//System.out.println("I'm calling from Node.ELEMENT_NODE");
System.out.print("<");
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
}
System.out.println(">");
NodeList children = node.getChildNodes();
if (children != null)
{
//System.out.println("Child node exists");
int len = children.getLength();
//System.out.println("length "+len);
for (int i = 0; i < len; i++){
//System.out.println("Before calling printDOMTree " + children.item(i));
printDOMTree(children.item(i));
}
}
else{
//System.out.println("No Children beyond");
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
// print text
case Node.TEXT_NODE:
{
System.out.print(node.getNodeValue());
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print("<?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
{
System.out.print("");
System.out.print(data);
}
System.out.print("?>");
break;
}
}
if (type == Node.ELEMENT_NODE)
{
System.out.println();
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
}
}
public ResultSet getResultSet()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Con = DriverManager.getConnection("jdbc racle:thin:@192.20.200.99:1521 reprod","scott","tiger");
//System.out.println(" Reached here!!!");
Statement stmt = Con.createStatement();
//System.out.println(" Reached here 2 !");
rs = stmt.executeQuery("select empno,ename, sal from emp");
}
catch(Exception e)
{
}
return rs;
}
public static void main(String args[])
{
buildXML bxml = new buildXML();
rs = bxml.getResultSet();
if (rs ==null)
{
System.out.println("SSSSSSSSSSSSSS");
}
else
{
System.out.println("dssadadasdsadsad");
}
try
{
Document doc = (Document)Class.forName("org.apache.xerces.dom.DocumentImpl").newInstance();
Element root = doc.createElement("emp");
while (rs.next())
{

Element empno = doc.createElement("empno");
empno.setAttribute("id" , rs.getString("empno"));

//empno.appendChild(doc.createTextNode(rs.getString("empno")));
root.appendChild(empno);
Element empname = doc.createElement("empname");
empname.appendChild(doc.createTextNode(rs.getString("ename")));
root.appendChild(empname);

Element sal = doc.createElement("sal");
sal.appendChild(doc.createTextNode(rs.getString("sal")));
root.appendChild(sal);
}
doc.appendChild(root);
bxml.printDOMTree(doc);
}
catch(Exception e)
{
e.printStackTrace();
}

}
}
===============================================================
Hope now U got what I was trying to achieve.
Pl reply
[This message has been edited by Paula, Nordine (edited May 09, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Building XML