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).]