XStream进行xml和bean互转
Posted sky-chen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XStream进行xml和bean互转相关的知识,希望对你有一定的参考价值。
加入pom
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.3</version>
</dependency>
- 最简单的使用方式
XStream xStream = new XStream();
//声明XStream注解来源
xStream.processAnnotations(FuiouResponse.class);
//xml 转bean
Object o = xStream.fromXML(xmlStr);
//bean 转xml
xStream.toXML(response)
- 需要指明注解
/**
* @author laoliangliang
* @date 2018/11/6 17:00
*/
@XStreamAlias("ap")
public class FuiouResponse {
@XStreamAlias("signature")
private String signature;
@XStreamAlias("plain")
private Plain plain;
public void setSignature(String signature) {
this.signature = signature;
}
public String getSignature() {
return signature;
}
public Plain getPlain() {
return plain;
}
public void setPlain(Plain plain) {
this.plain = plain;
}
public class Plain {
@XStreamAlias("resp_code")
private String respCode;
@XStreamAlias("resp_desc")
private String respDesc;
public String getRespCode() {
return respCode;
}
public void setRespCode(String respCode) {
this.respCode = respCode;
}
public String getRespDesc() {
return respDesc;
}
public void setRespDesc(String respDesc) {
this.respDesc = respDesc;
}
}
}
和jpa类似,取个别名
以上是关于XStream进行xml和bean互转的主要内容,如果未能解决你的问题,请参考以下文章