1. Understand XML parsing technology
1.1. XML related concepts
(1) DTD: XML syntax rules, which are the verification mechanism of XML files. You can compare XML documents with DTD files to see if the document complies with the specifications and whether the elements and labels are used correctly.
(2) XML is the basis of SOA.
1.2. XML processing technology
(1) In order to use XML, we need to access data through an XML processor or XMLAPI. Currently, JAXP provides two methods for processing XML: DOM and SAX.
① DOM: DOM accesses data and structures in XML documents programmatically, based on the tree structure of XML documents in memory. The disadvantage is that it requires a lot of overhead to load the entire XML document into memory.
②SAX: Based on event-driven, one segment is used to parse one segment, which solves the problem of DOM taking up a lot of memory, but its disadvantage is that it cannot access documents randomly.
(2) In order to solve the problems of DOM and SAX, a stream-based StreamAPIforXML (StAX for short) has appeared. It has been added to JAXP1.4 of JDK6, and StAX is also event-driven.
(3) DOM, SAX and StAX all process XML from the document structure, but many applications only focus on the document data itself, so XML data binding technology came into being.
Data binding: refers to the process of extracting data from storage media (XML documents and databases) and representing this data through programs, that is, binding the data to a memory structure that the virtual machine can understand and operate.
XML binding framework: Castor, JAXB, JiBX, Quick, Zeus, etc.
2. XML processing tool: XStream
2.1. XStream Overview
(1) XStream is a simple and easy-to-use open source framework for serializing Java objects into XML, or deserializing XML into Java objects. Key Features of XStream:
(2) XStream architecture composition:
Converters: When XStream encounters an object that needs to be converted, it is delegated to the appropriate converter implementation.
IO (input/output): XStream is abstracted from the underlying XML data through HierarchicalStreamWriter and HierarchicalStreamReader, and is used for serialization and deserialization operations respectively.
Context: When XStream serializes deserialization objects, two classes MarshallingContext and UnmarshallingContext are created, and the tower gate processes the data and delegates it to the appropriate converter.
Facade (Unified Entrance): Integrate the above 3 points together and open them to users with a unified interface.
2.2. Quick Start
(1) Create an XStream and specify an XML parser
XStreamxstream=newXStream(newDomDriver());
If you do not specify a parser, XStream will use XPP (XMLPullParser) parser by default, which is a high-speed parser.
(2) The examples are as follows:
2.3. Use XStream alias
(1) In the above example, the full class name of the java object corresponds to the root element of the XML file, and the attribute name corresponds to the node element of the XML file. However, in actual situations, both the java object and the XML object may have already defined names, so alias mapping is needed.
XStream has 3 alias configurations:
Category name: Use alias(Stringname,Classtype).
Class member alias: Use aliasField(Stringalias,ClassdefinedIn,StringfieldName).
Class members are used as attribute alias: Use aliasAttribute(ClassdefinedIn,StringattributeName,Stringalias), and naming them alone has no meaning, and they must also be applied to a certain class through useAttributeFor(ClassdefinedIn,StringfieldName).
(2) Modify the example in 2.2 through alias:
2.4. XStream Converter
During the development process, sometimes you need to convert some custom types. Just implement the Converter interface and call the registerConverter() method of XStream to register the converter.
2.5. XStream annotation
(1) Common annotations for XStream are as follows:
(2) Use
XStreamxstream=newXStream(newDomDriver());
There are 2 ways to load objects:
①Method 1:
xstream.processAnnotations(AAA.class);
xstream.processAnnotations(BBB.class);
② Method 2:
xstream.autodetectAnnotations(true);//Auto load the annotated bean and also caches the annotated object.
2.6. Flowing objects
(1) XStream provides alternative implementations for ObjectInputStream and ObjectOutputStream, allowing XML serialization or deserialization operations in the form of object streams. The previous one is XML read by the DOM-based XML parser, here we should obviously use the stream method for parsing.
Examples are as follows:
(2) The difference between using PrettyWriter and CompactWriter is that PrettyWriter will format the generated XML, while CompactWriter will compress the generated XML.
2.7. Persistence API
(1) XStream provides a simple way to persist objects in the collection into files, such as: XmlArrayList, XmlSet, XmlMap, etc.
(2) Before creating a collection, you also need to specify a persistence strategy PersistenceStrategy.
2.8. Handle JSON
(1) XML has an unshakable position in WebService, but in most web applications, lightweight JSON is still used as the data exchange format.
(2) XStream provides JettisonMappedXmlDriver and JsonHierarchicalStreamDriver to complete the conversion of java objects and json.
Examples are as follows:
(3) The difference between JettisonMappedXmlDriver and JsonHierarchicalStreamDriver:
①JettisonMappedXmlDriver generates compressed JSON, while JsonHierarchicalStreamDriver generates formatted JSON.
② If you want to convert JSON to an object, you can only use JettisonMappedXmlDriver.
3. Other common O/XMapping open source projects
Comparison of JAXB, XMLBeans, Cstor, JiBX:
4. Integration with SpringOXM
4.1. Overview of SpringOXM
(1) SpringOXM has made a unified abstraction and encapsulation of the mainstream O/XMapping framework. Marshaller and Unmarshaller are SpringOXM's two core interfaces. Marshaller is used to convert objects into XML, and Unmarshaller is used to convert XML into objects.
(2) The O/XMapping component wrappers are as follows:
4.2. Configure in Spring
(1) XStreamMarshaller configuration instance:
5. Summary
(1) XML data binding of java applications can be summarized into 2 ways:
Generate Java language code (such as JAXB, XMLBeans, Castor) based on XML documents.
Use some form of mapping binding method, that is, set how Java classes are associated with XML (such as XStream, Castor, JiBX).
(2) Comparison of 2 ways:
Using a stable document structure defined by Schema or DTD, and the structure is suitable for the needs of the application, a code generation method may be the best choice.
If you use an existing Java class, or if you want to use a structure of a class that reflects the application's usage of data, rather than an XML structure, the mapping method is the best choice.
Summarize
The above is all about spring using OXM for object XML mapping analysis. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!