• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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>



0
chaitu chaitu
Asked:
chaitu chaitu
  • 6
  • 4
  • 4
  • +4
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 EikelmanCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 4
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now