• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Building XML

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can u send me a sample code please.
I want to create an XML document dynamically with dat coming from database
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Paula, Nordine
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic