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;


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.

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

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 ) ;
chaitu chaituAuthor Commented:
if there are 100 records coming from a table its very tedious job to write like that;
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.
chaitu chaituAuthor Commented:
what CEHJ posted above do the same thing.any other alternative?
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?
chaitu chaituAuthor Commented:
now xml file creatied like this ;my question is if want to change  element node names  like lowercase etc,..

Mayank SAssociate Director - Product EngineeringCommented:
You can keep them in a constants-file or read them from a properties-file (better).
chaitu chaituAuthor Commented:
anybody know abt  XMLFUNCTION in oracle??
Mayank SAssociate Director - Product EngineeringCommented:
You mean this?

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
Mayank SAssociate Director - Product EngineeringCommented:
You can also keep the name of the nodes in the DB as a column ;-O
Look at

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

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.*;

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

        // Set some attributes for George the Giraffe

        // Add the animals to the animals set

        // Add the animals set to the zoo (root XML element)

        // Set the root element of our document

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


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


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.