1. Introduction to XStream
1. The function of XStream: mutual conversion between Java objects and XML.
2. Features of XStream: flexible and easy to use, high performance, clean XML, custom transformation strategy, error diagnosis, etc.
3. XStream usage scenarios: transformation; persistent objects; configuration; unit testing.
4. Dependent packages: xstream-[version].jar, xpp3-[version].jar, xmlpull-[version].jar
5. Configuration of pom.xml file in MAVEN
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
Second, the use of XStream
1. Create an XStream instance
XStream xstream = new XStream();
XStream xstream = new XStream(new DomDriver());
XStream xstream = new XStream(new StaxDriver());
2. Common methods
xstream.toXML(): Convert the object to XML
xstream.fromXML(): convert XML to object
xstream.alias(): create an alias for the specified class
xstream.useAttributeFor(): set attributes
xstream.aliasField(): set an alias for the field
xstream.aliasAttribute(): set an alias for the attribute
xstream.addImplicitCollection(): The collection node is hidden, only the child information nodes are displayed
xstream.processAnnotations(): Process Annotations information
xstream.autodetectAnnotations(): Automatically detect annotations
3. Commonly used annotations
@XStreamAlias("message"): Set aliases for classes and fields
@XStreamImplicit(itemFieldName="part"): Implicit collection annotation
@XStreamAsAttribute: Convert to attribute
@XStreamOmitField: fields that do not correspond to XML and need to be omitted
@XStreamConverter(): inject converter
3. Code example
1. Suppose there is the following XML content to be converted into JAVA objects
<?xml version="1.0" encoding="UTF-8"?> <table name="ts_user" comment="User information"> <column name="id" type="char" length="10" comment="ID"></column> <column name="name" type="varchar" length="50" comment="用户名"></column> <column name="password" type="varchar" length="100" comment="密码"></column> <column name="age" type="int" length="11" comment="年龄"></column> <column name="createDate" type="date" comment="创建日期"></column> </table>
2. Create the corresponding model class
import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; import com.thoughtworks.xstream.annotations.XStreamImplicit; @XStreamAlias("table") public class Table { @XStreamAsAttribute private String name; @XStreamAsAttribute private String comment; @XStreamImplicit(itemFieldName="column") private List<Column> columnList; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public List<Column> getColumnList() { return columnList; } public void setColumnList(List<Column> columnList) { this.columnList = columnList; } } import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; @XStreamAlias("column") public class Column { @XStreamAsAttribute private String name; @XStreamAsAttribute private String type; @XStreamAsAttribute private Integer length; @XStreamAsAttribute private String comment; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Integer getLength() { return length; } public void setLength(Integer length) { this.length = length; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
3. Convert the code
XStream xstream = new XStream(); xstream.processAnnotations(new Class[]{Table.class, Column.class}); Table table = (Table)xstream.fromXML(xmlContent); // xmlContent is xml content