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