7

Possible Duplicate:
What is the best/simplest way to read in an XML file in Java application?

How do I convert XML to POJO and vice versa? does axis2 provide that capability? or does java already have built-in capability for this? or any other framework? thanks

Community
  • 1
  • 1
Java Developer
  • 95
  • 1
  • 1
  • 6
  • 1
    http://stackoverflow.com/questions/607141/what-is-jaxb-and-why-would-i-use-it – d1e Jun 27 '12 at 07:50

5 Answers5

7

I really encourage you to use JAXB.

JAXB is an annotation framework that maps Java classes to XML and XML schema (and viceversa). It is extremely useful because instead of interacting with an abstract representation of an XML document, you can work with real Java objects that are closer to the domain you are modeling.

If you alos need to build a RESTful web-service, with automatic serialization/deserialization of Java Objects into XML, through JAXB, I also suggest you the reading of this book:

Oreilly RESTful Java with JAX-RS - (Chapter 6. JAX-RS Content Handlers)

user278064
  • 9,982
  • 1
  • 33
  • 46
  • it's actually bundled with the jdk (not sure about the jre though). here's an example, it seems really easy to use: http://www.java2s.com/Code/Java/JDK-6/MarshalJavaobjecttoxmlandoutputtoconsole.htm – kritzikratzi Jun 27 '12 at 08:42
  • maybe forget my comment, it's even easier... posting in a different answer – kritzikratzi Jun 27 '12 at 08:48
  • +1 for JAXB. Also since it is a standard (JSR-222) there are additional implementations such as EclipseLink MOXy: http://www.eclipse.org/eclipselink/moxy.php (I'm the tech lead). – bdoughan Jun 27 '12 at 10:32
6

If you need a readily available framework for the same, please have a look at xstream

Felix Christy
  • 2,179
  • 1
  • 19
  • 32
2

There are various frameworks: Jaxb, XStream, JiBX etc.

Tutorials that might help you:

http://thomassundberg.wordpress.com/2010/01/19/how-to-convert-a-pojo-to-xml-with-jaxb/

http://numberformat.wordpress.com/2009/11/01/using-jaxb-to-convert-between-xml-and-pojos/

http://soadev.blogspot.com/2011/07/jaxb-converting-pojo-to-xml-and-vice.html

http://jibx.sourceforge.net/binding/tutorial/binding-tutorial.html

There's an article here, that explains how to do it with only XPath.

Also check out this similar discussion on SO here.

Community
  • 1
  • 1
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
2

a simple version of this is built into java >=1.4 using the XMLEncoder and XMLDecoder classes.


a quick example

usage is quite simple, along the lines of

XMLEncoder xmlEncoder = new XMLEncoder( outputStream );
xmlEncoder.writeObject( myObject );

will give you something like

<?xml version="1.0" encoding="UTF-8"?> 
<java> 
 <object class="your.class.Name"> 
  <void property="fieldName"> 
   <boolean>true</boolean> 
  </void> 
  etc. etc. etc. 
 </object> 
</java> 

to read back the object you simply do

XMLDecoder xmlDecoder = new XMLDecoder( inputStream );
MyClass thing = (MyClass) xmlDecoder.readObject();

here's a random tutorial i found on google:
http://www.avajava.com/tutorials/lessons/how-do-i-write-a-javabean-to-an-xml-file-using-xmlencoder.html

this method is not amazingly flexible, but it's built in, configuration free and very predictable. might be a good starting point.

some additional notes:

here's a document that outlines the xml format: http://java.sun.com/products/jfc/tsc/articles/persistence3/

and here is another link i just found, this explains how to move from xmlencoder to jaxb (built into jdk >= 1.6) for more flexibility: http://en.newinstance.it/2010/08/05/javabeans-to-xml-with-no-libraries/

kritzikratzi
  • 19,662
  • 1
  • 29
  • 40
1

You can try JAXB http://jaxb.java.net/ or XMLBeans http://xmlbeans.apache.org/

greuze
  • 4,250
  • 5
  • 43
  • 62