Home > Uncategorized > XML result with Java query from Database

XML result with Java query from Database

Hi, This post is about to how to generate xml result or output from PostgreSQL database with query i mean :).  In PostgreSQL 8.4 there is sql xml query by which we can generate the xml result from table or tables. the sql query goes like this

select xmlelement (name Item, c.item)
from inventory c where itemname='inkpen';

In my case I had to generate xml result into file with my Java application for later use into web services . Below is the partly shown code ran in java application . what is does run query against table and bring result in XML format later file is created and the result is stored in that file.

String sqlxmlquery = select xmlelement(name Sensor,xmlelement(name NodeId, c.nodeid),"+
"xmlelement(name Xaxis, c.xaxis),xmlelement(name Yaxis, c.yaxis),"+
"xmlelement(name Zaxis, c.zaxis),xmlelement(name Temp, c.temp),"+
"xmlelement(name Light, c.light),"+
"xmlelement(name Time, c.modtime)) from spot_result c where nodeid='0014.4F01.0000.6A40';";

//execute query
ResultSet result1 = select.executeQuery(spotquery1);

//condition loop to print the update result all the time
while (result1.next()) {
    String outputsunspot= result1.getString(1);

  //create file
    File spot1 = new File("C:/Documents and Settings/Admin/Desktop/webdump/xml_sensor_result/spotresult1.xml");

//write the result in file
    Writer writer = new BufferedWriter(new FileWriter(spot1));

//not sure with this one - writer should be closed i guess :)

Thats it for now ! if you need to know more , criticize me . most welcome to query on my inbox at mubeen44us at gmail.com cheers!

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: