3

I asked this question before:

Generate XML from a class

I want to do this with Java.

Is it possible to do the same with Java via attributes?

or is there a framework i can use for this purpose as well.

Community
  • 1
  • 1
DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • 2
    Dup: http://stackoverflow.com/questions/736343/what-is-the-best-way-to-convert-a-java-object-to-xml-with-open-source-apis – skaffman Mar 20 '12 at 14:30

3 Answers3

8

XStream will allow you to represent any class as an XML. You can check here for an example.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • +1 Using XStream would be the simplest and easiest on this occassion. – asgs Mar 20 '12 at 14:34
  • One of the first dependencies I add to a new project. Even without any XML need: very useful for debugging ! – Guillaume Mar 20 '12 at 14:37
  • Cool. That IS a really simple solution. I've never read about xstream until now. – JohnnyK Mar 20 '12 at 14:38
  • @Jakrabbit: It is usually very simple and straightforward to use. If I am not mistaken XML generation can be controlled, to a certain extent, through the use of annotations. – npinti Mar 20 '12 at 14:41
  • @DarthVader - For a JAXB (JSR-222)/XStream comparison see: http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html. Note I'm the EclipseLink JAXB (MOXy) lead. – bdoughan Mar 20 '12 at 15:37
5

JAXB could be what you want. It's fairly common. You just add some annotations to your properties and any instance of that class can be serialized/deserialized to XML.

JohnnyK
  • 1,103
  • 6
  • 10
  • +1 for JAXB (JSR-222). JAXB implementations (Metro, MOXy, etc) do not require any annotations, they are only needed to customize the default XML representation: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics – bdoughan Mar 20 '12 at 15:18
0

I prefer XStream, but there is also Castor XML

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101