0

We are using hibernate to load data from oracle database. I need to load the data from one of the tables and then store selected data from this table as an xml file in another table. It would be great if someone could suggest, what would be the best way to achieve this functionality?

bdoughan
  • 147,609
  • 23
  • 300
  • 400
awsome
  • 2,143
  • 2
  • 23
  • 41

5 Answers5

4

Take a look at this question which discusses libraries (such as JAXB and XStream) you can use to convert Java objects to XML.

What is the best way to convert a java object to xml with open source apis

Community
  • 1
  • 1
dogbane
  • 266,786
  • 75
  • 396
  • 414
1

Use Xstream of ThougthWorks.

"XStream is a simple library to serialize objects to XML and back again."

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
  • 1
    In this question the objects are actually JPA entities, since XStream uses field access to process objects this can cause issues with lazy loaded properties (see http://stackoverflow.com/questions/328832/issue-with-serializing-hibernate-objects-using-xstream). – bdoughan Nov 26 '10 at 19:32
1

XMLEncoder (java.beans.XMLEncoder) is already included in the JDK and enables you to persist java objects to XML without any external libraries or tools.

An example class:

public class Foo {
   private String foo ;

   public void setFoo(String s) {
     foo = s;
   }

   public String getFoo() {
     return foo;
   }
}

Helper class to serialize to XML:

import java.beans.XMLEncoder;
import java.io.*;

public class FooHelper {
    public static void write(Foo f, String filename) throws Exception{
        XMLEncoder encoder =
           new XMLEncoder(
              new BufferedOutputStream(
                new FileOutputStream(filename)));
        encoder.writeObject(f);
        encoder.close();
    }
}

The resulting XML:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.5.0-beta" class="java.beans.XMLDecoder"> 
 <object class="Foo"> 
  <void property="foo"> 
   <string>bar</string> 
  </void> 
 </object> 
</java>
Martin Gross
  • 929
  • 7
  • 5
1

If you are using JPA to persist your entities then look at if you can switch your provider to EclipseLink. If can convert the the same JPA persistent pojos to XML for you. So you will only have to deal with single library.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • +1, check out my blog for more examples on using EclipseLink JAXB (MOXy): http://bdoughan.blogspot.com/ – bdoughan Nov 26 '10 at 19:23
-1

You can take a look at xmlbeans

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • XMLBeans generates its own object model and can not be used with the existing entities described in this question. – bdoughan Nov 26 '10 at 19:33