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>



LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Since (AFAIK) you're using Oracle, you could use their classes to generate XML, but here's a naive (but probably effective) implementation:

http://www.rgagnon.com/javadetails/java-0511.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
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 ) ;
0
chaitu chaituAuthor Commented:
if there are 100 records coming from a table its very tedious job to write like that;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Mayank SAssociate Director - Product EngineeringCommented:
You can put it in a loop.
0
chaitu chaituAuthor Commented:
what CEHJ posted above do the same thing.any other alternative?
0
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
chaitu chaituAuthor Commented:
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>
0
Mayank SAssociate Director - Product EngineeringCommented:
You can keep them in a constants-file or read them from a properties-file (better).
0
chaitu chaituAuthor Commented:
anybody know abt  XMLFUNCTION in oracle??
0
Mayank SAssociate Director - Product EngineeringCommented:
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....
0
CEHJCommented:
>> what CEHJ posted above do the same thing.any other alternative?

No - the node names are generated automatically - no tedium involved at all
0
CEHJCommented:
>>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
0
Mayank SAssociate Director - Product EngineeringCommented:
You can also keep the name of the nodes in the DB as a column ;-O
0
mrigankCommented:
Look at http://www.castor.org/

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

________
radarsh
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.