Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read and write the data in XML file

 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ALL,

Below given the code i am retrive from Database.

Could you please tell me how this data should be read and write in XML file.



Please suggest.

Thanks in advance.
Regards,
Sumanta
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several APIs in Java for working with XML.

Unfortunately, the standard Java APIs for working with XML, in the package javax.xml and subpackages, are not the easiest and best to use in many cases. The standard APIs seem to be more focused on parsing XML and are not very good at outputting XML.

There are many alternatives, such as XOM (which XML guru Michael Kay said good things about on the Devoxx conference where I was recently), JDOM, XStream and many others.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using the Oracle Database you can use the following pacakage.

select dbms_xmlgen.getxml('select sysdate from dual',0)xml from dual;

In the place of the 'select sysdate from dual' you can specify your query.

The data will automatically be generated in XML. You can also use the DOM Parser in java if this doesn't suit your requirement.
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Below given is the code.I am retrive the data from database and trying to export in the XML file.
I didnot get any compilation error.But i dont know why in my code the data is not export in the xml file.
hERE I TRIED WITH FNAME for column.

Please suggest me how i will fix this issue.



Thanks in advance.

Regards,
Sumanta Panda
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't ever close the output stream. You should do that.

Also, do you only have one record to output? If not, then you are reusing the same file for all records and you are going to end up with one file containing only the last record.
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Paul Clapham,

As per your advise i have closed the conncetion.Still i am getting the same issue(Data is not export in the file.

Please suggest me for the needful.



Thanks for your time.

Regards,
Sumanta
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fos = new FileOutputStream(fileName);

Do you really want to create a new file for each DB record? Do you have code somewhere to assign them different filenames?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another guess: your ResultSet doesn't contain any records. But this is a simple thing which you could find out for yourself by a little bit of debugging. Have you done any of that? Do you know which lines of code are being executed?
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you retrieve data from database using Hibernate ORM Framework, then all you need to do to get XML-based version is call toXML() on a single object.

Very easy! Hibernate is Great!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic