chaitu chaitu
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>
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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?
ASKER
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>
<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).
ASKER
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....
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
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
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
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("no se","long" );
eElephant.setAttribute("ne ck","short ");
eElephant.setText("Edward" );
// Set some attributes for George the Giraffe
eGiraffe.setAttribute("nos e","short" );
eGiraffe.setAttribute("nec k","long") ;
eGiraffe.setText("George") ;
// Add the animals to the animals set
eAnimals.addContent(eEleph ant);
eAnimals.addContent(eGiraf fe);
// Add the animals set to the zoo (root XML element)
eRoot.addContent(eAnimals) ;
// Set the root element of our document
docTest.setRootElement(eRo ot);
// Output our XML document to file
try {
fOutputFile = new FileOutputStream("test.xml ");
xmlOut.output(docTest,fOut putFile);
} 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
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("no
eElephant.setAttribute("ne
eElephant.setText("Edward"
// Set some attributes for George the Giraffe
eGiraffe.setAttribute("nos
eGiraffe.setAttribute("nec
eGiraffe.setText("George")
// Add the animals to the animals set
eAnimals.addContent(eEleph
eAnimals.addContent(eGiraf
// Add the animals set to the zoo (root XML element)
eRoot.addContent(eAnimals)
// Set the root element of our document
docTest.setRootElement(eRo
// Output our XML document to file
try {
fOutputFile = new FileOutputStream("test.xml
xmlOut.output(docTest,fOut
} catch(Exception e) {
System.out.println("Failed
}
return;
}
}
The setText method can be used to set the text you received from the SQL query.
Cheers,
Leo
:-)
StringReader reader = new StringReader ( "<Root/>" ) ;
InputSource source = new InputSource ( reader ) ;
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.new
DocumentBuilder documentBuilder = documentFactory.newDocumen
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 ) ;