Use XStream (XML transfer JAVA)

 

XStream is a tool for Java objects and XML mutual conversion. Provides support for all foundation types, arrays, collections, and other types of direct conversion. Therefore, commonly used in XML data exchange, object serialization (this serialization and serialization technology Java objects are essentially different).

Feature

  • Easy to use - XStream API that provides a high-level look to simplify common use cases.

  • No need to create a map - XStream API that provides a default mapping of most of the target sequence.

  • Performance - XStream quick and low memory footprint, suitable for large object graphs or systems.

  • Clean XML - XStream to create a clean and compact XML results, it is very easy to read.

  • You do not need to modify the object - XStream serializable internal fields, such as private and final field, and a non-public internal support classes. The default constructor is not a mandatory requirement.

  • Full object graph support - XStream allowed to remain in the repeated references to the object model encountered, and to support circular references.

  • Self-defined conversion strategy - custom policy may allow a specific type of customization is expressed as a registered XML.

  • Security framework - XStream provides a fair solution control group related to the type of input in order to prevent manipulation of security concerns.

  • Error message - is abnormal due to malformed XML, XStream throws a single exception, provides detailed diagnostics to solve this problem.

  • Another output formats - XStream support other output formats, such as JSON.

     

XStream converter between Java objects and objects quite XML, the conversion process is bidirectional. Creating XSteam object is very simple, just new XStream () can be.

              Java to xml, with toXML () method.  

              Xml to Java, with fromXML () method. 

2. Operation

(1) introducing the desired dependencies

 <dependency>            <groupId>com.thoughtworks.xstream</groupId>            <artifactId>xstream</artifactId>            <version>1.4.10</version> </dependency>

(2) using the same general approach, on the need to add annotations @XStreamAlias ​​entity class and the attributes (), so that the properties of entity class XML attributes corresponding to the converted, so that the conversion

@Entity@Table(name = "oa_document")@XStreamAlias("META_DATA")public class OADocument  {    //印章id    @XStreamAlias("SEAL_ID")    @JsonProperty("sealId")    @Column(length = 22, nullable = false)    private String sealId;
    //用印描述    @XStreamAlias("SEAL_DESC")    @Column(name = "seal_desc", length = 256)    private String sealDesc;
    //文档编号    @XStreamAlias("FILE_NO")    @Column(name = "file_no", length = 64)    private String fileNo;    ........
操作OADocument oaDocument= XStreamUtil.getXmlToEntity(xml,oaDocument,classes);类:XStreamUtil   public static <T> T getXmlToEntity(String xmlStr, T targetObj, Class<?>[] classes)throws Exception{        XStream xstream = new XStream();        //xstream安全设置        XStream.setupDefaultSecurity(xstream);        //应用OADocument类的注解        xstream.processAnnotations(OADocument.class);        xstream.allowTypes(classes);        //自动检测注解        xstream.autodetectAnnotations(true);        targetObj=(T)xstream.fromXML(xmlStr);        return targetObj;    }

result:

reference:

        XStream has a very powerful, you can refer to:

    https://blog.csdn.net/qq_35464899/article/details/85047107

    https://blog.csdn.net/xiaokui_wingfly/article/details/46470145

发布了15 篇原创文章 · 获赞 2 · 访问量 4213

Guess you like

Origin blog.csdn.net/weixin_41858337/article/details/104716781