Saving Java Object Graphs as XML file

前端 未结 15 2549
耶瑟儿~
耶瑟儿~ 2021-02-07 12:13

What\'s the simplest-to-use techonlogy available to save an arbitrary Java object graph as an XML file (and to be able to rehydrate the objects later)?

相关标签:
15条回答
  • 2021-02-07 12:45

    If you need to control the structure of the XML, the XStream is a good choice. You can use annotations to define precisely the structure/mapping of the XML and your objects.

    0 讨论(0)
  • 2021-02-07 12:46

    XStream by the folks at Thoughtworks has a simple API and even deals with things like duplicate and circular references. It seems to be actively developed and is well documented.

    http://x-stream.github.io/

    0 讨论(0)
  • 2021-02-07 12:55

    The easiest way here is to serialize the object graph. Java 1.4 has built in support for serialization as XML.

    A solution I have used successfully is XStream (http://x-stream.github.io/)- it's a small library that will easily allow you to serialize and deserialize to and from XML.

    The downside is you can only very limited define the resulting XML; which might not be neccessary in your case.

    0 讨论(0)
  • 2021-02-07 12:58

    If you are really only interested in serializing your objects to a file and then deserializing them later, then you might check out YAML instead of XML. YAML is much easier to work with than XML and the output files are very human-readable (which may or may not be a requirement). Check out yaml.org for more information. I've used JYAML successfully on a recent project.

    0 讨论(0)
  • 2021-02-07 13:03

    JAX-B is part of the standard APIs and really easy to use.

    0 讨论(0)
  • 2021-02-07 13:04

    If you need control over the XML that gets generated, I recommend taking a look at Betwixt (http://commons.apache.org/betwixt/) - it adds a lot of functionality to Apache's digester (Digester is good for building object graphs from XML, but is not so good for generating them).

    If you really don't care about the XML that gets generated (just that it can be deserialized in the future), then the XMLEncoder/Decoder classes built into Java or good - as long as the objects you are serializing follow the JavaBean specification. The biggest area I've run into problems with the XMLEncoder/Decoder solution is if you have a bean that returns an immutable list for one of it's properties - the encoder doesn't handle that situation very well.

    0 讨论(0)
提交回复
热议问题