Saving Java Object Graphs as XML file

前端 未结 15 2552
耶瑟儿~
耶瑟儿~ 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 13:05

    Jackson

    The Jackson Project is a processing and binding library for XML, JSON, and some other formats.

    … Jackson is a suite of data-processing tools for Java (and the JVM platform), including the flagship streaming JSON parser / generator library, matching data-binding library (POJOs to and from JSON) and additional data format modules to process data encoded in Avro, BSON, CBOR, CSV, Smile, (Java) Properties, Protobuf, XML or YAML; and even the large set of data format modules to support data types of widely used data types such as Guava, Joda, PCollections and many, many more…

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

    The Simple API is, well, simple! It's really good. http://simple.sourceforge.net/

    You can also use XStream: http://www.ibm.com/developerworks/library/x-xstream/index.html

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

    XStream is very simple http://x-stream.github.io/

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

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

    Use java.beans.XMLEncoder. Its API is very simple (actually a little too simple; it'd be nice to wire it to a SAX ContentHandler), but it works on many graphs out of the box, and it's easy to create your own persistence delegate for any odd-ball classes you might encounter.

    • The syntax used by XMLDecoder allows you to invoke any method, instance or static, including constructors, so it's extremely flexible.
    • Other encoders name elements and attributes after class and field names, so there's no fixed schema for the result. The XMLEncoder's XML follows a simple DTD and can easily be validated or transformed, even when you've never seen the types it uses.
    • You can assign objects an identifier, and reference them throughout the graph.
    • You can refer to constants defined in classes or interfaces.

    And, it's built into Java SE, so you don't need to ship an extra library.

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

    Apache digester is fairly easy: http://commons.apache.org/digester/
    JAXB is newer and comes with annotation goodness: https://jaxb.dev.java.net

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

    Simple

    Although XStream and JAXB can serialize an some object graphs succssfully they can not handle very complex graphs. The most powerful solution for large complex graphs is Simple XML Serialization. It can handle any graph. Also, it’s fast and simple to use without any dependencies.

    To quote the Simple project page:

    Simple is a high performance XML serialization and configuration framework for Java. Its goal is to provide an XML framework that enables rapid development of XML configuration and communication systems. This framework aids the development of XML systems with minimal effort and reduced errors. It offers full object serialization and deserialization, maintaining each reference encountered. In essence it is similar to C# XML serialization for the Java platform, but offers additional features for interception and manipulation.

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