Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

generate xml file with data getting from that sql query

i will retreive records from emp table in java class say if query is like this select id,name,address from emp

i want to generate xml file with data getting from that query and place somewhere in c directory; how can i do this in java class;

<id>1</id>
<name>1</name>
<address>1</address>



ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can generate an XML using DOM/ SAX/ JDOM once you have the values from the DB, e.g.:

StringReader reader = new StringReader ( "<Root/>" ) ;
InputSource source = new InputSource ( reader ) ;
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance () ;
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder () ;
doc = documentBuilder.parse ( source ) ; // create a document and load it with the initial XML
Element rootNode = doc.getDocumentElement () ;
Node parentNode = doc.createElement ( "ID" ) ;
Node childNode = doc.createTextNode ( "ID" ) ;
childTagNode.setNodeValue ( value from DB ) ;
rootNode.appendChild ( parentNode ) ;
parentNode .appendChild ( childNode ) ;
Avatar of chaitu chaitu

ASKER

if there are 100 records coming from a table its very tedious job to write like that;
You can put it in a loop.
what CEHJ posted above do the same thing.any other alternative?
Not sure, I didn't go through that link because I thought it was some API provided by Oracle. Otherwise, you will need to use one of the Java parsers like SAX/ DOM/ JDOM.... anyway, putting what I posted inside a loop to do what you have to do should not take more than 15-20 lines of code?
now xml file creatied like this ;my question is if want to change  element node names  like lowercase etc,..

   <Row>
      <ID1>7</ID1>
      <NAME>ryuru88888</NAME>
      <ADDRESS>tutu</ADDRESS>
      <EMAIL>TUTUTU</EMAIL>
      <PHONE>TUTUTU</PHONE>
   </Row>
You can keep them in a constants-file or read them from a properties-file (better).
anybody know abt  XMLFUNCTION in oracle??
You mean this?

http://www.oracle.com/technology/oramag/oracle/03-may/o33xml.html

I wouldn't recommend using too many DB-specific features because it becomes a problem later, if you want to switch/ migrate from Oracle to something else....
>> what CEHJ posted above do the same thing.any other alternative?

No - the node names are generated automatically - no tedium involved at all
>>now xml file creatied like this ;my question is if want to change  element node names  like lowercase etc,..

Using the code example to which i posted the link, it would be a simple matter of aliasing the columns in your query

select id as Identifier from sometable
You can also keep the name of the nodes in the DB as a column ;-O
Avatar of mrigank
mrigank

Look at http://www.castor.org/

Youll need to create a mapping file to map the Java objecs to xml.
For more information, see http:Q_21787929.html

________
radarsh
I think this is a duplicate post.  
here take a look at this

The following is an example of how to use the JDOM library to output a simple XML document:

import org.jdom.*;
import org.jdom.output.*;
import java.io.*;

public class XMLExample {
    public static void main(String[] args) {
        // Create an output stream for our file I/O
        FileOutputStream fOutputFile;

        // Create the required JDOM document and elements
        Document docTest = new Document();
        Element eRoot = new Element("zoo");
        Element eAnimals = new Element("animals");
        Element eElephant = new Element("elephant");
        Element eGiraffe = new Element("giraffe");

        // Create the XMLOutputter which writes the XML document to the file.
        XMLOutputter xmlOut = new XMLOutputter();

        // Set some attributes for Edward the Elephant
        eElephant.setAttribute("nose","long");
        eElephant.setAttribute("neck","short");
        eElephant.setText("Edward");

        // Set some attributes for George the Giraffe
        eGiraffe.setAttribute("nose","short");
        eGiraffe.setAttribute("neck","long");
        eGiraffe.setText("George");

        // Add the animals to the animals set
        eAnimals.addContent(eElephant);
        eAnimals.addContent(eGiraffe);

        // Add the animals set to the zoo (root XML element)
        eRoot.addContent(eAnimals);

        // Set the root element of our document
        docTest.setRootElement(eRoot);

        // Output our XML document to file
        try {
            fOutputFile = new FileOutputStream("test.xml");
            xmlOut.output(docTest,fOutputFile);
        } catch(Exception e) {
            System.out.println("Failed to write out XML data.");
        }

        return;
    }
}


The setText method can be used to set the text you received from the SQL query.


Cheers,

Leo
:-)