maven依赖
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.10</version> </dependency>
xml转javabean对象 ,在转换List对象的时候,没有根节点的情况下转换
xml 如下:
<mail>
<from>123</from>
<to>45p6</to>
<body id="cn">
<sysCode>sysCode1</sysCode>
<sysName>sysName1</sysName>
</body>
<body id="en">
<sysCode>sysCode2</sysCode>
<sysName>sysName2</sysName>
</body>
</mail>
Mail.java
package vo;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import java.util.List;
/**
* Created by edison on 2018/5/7.
*/
@XStreamAlias("mail")
public class Mail {
private String from;
private String to;
@XStreamImplicit(itemFieldName="body")
private List<MailBody> mailBody;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public List<MailBody> getMailBody() {
return mailBody;
}
public void setMailBody(List<MailBody> mailBody) {
this.mailBody = mailBody;
}
}
注意:Mail类中 @XStreamImplicit(itemFieldName="body")该注解作用就是去掉根节点标签<mailBody>,直接显示列表成员标签, 如果没有这个注解 ,xml需要改成如下
<mail>
<from>123</from>
<to>45p6</to>
<mailBody>
<body id="cn">
<sysCode>sysCode1</sysCode>
<sysName>sysName1</sysName>
</body>
<body id="en">
<sysCode>sysCode2</sysCode>
<sysName>sysName2</sysName>
</body>
</mailBody>
</mail>
MailBody.java
package vo;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
/**
* Created by edison on 2018/5/7.
*/
@XStreamAlias("body")
public class MailBody {
@XStreamAsAttribute
private String id;
private String sysCode;
private String sysName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSysCode() {
return sysCode;
}
public void setSysCode(String sysCode) {
this.sysCode = sysCode;
}
public String getSysName() {
return sysName;
}
public void setSysName(String sysName) {
this.sysName = sysName;
}
}
测试类MailTest.java
package jackjson;
import com.thoughtworks.xstream.XStream;
import org.junit.Test;
import vo.Mail;
import vo.MailBody;
import java.util.ArrayList;
import java.util.List;
/**
* Created by edison on 2018/5/7.
*/
public class MailTest {
@Test
public void testBean2Xml(){
XStream xstream = new XStream();
XStream.setupDefaultSecurity(xstream);
xstream.autodetectAnnotations(true);
List<MailBody> list = new ArrayList<MailBody>();
MailBody body1 = new MailBody();
body1.setId("cn");
body1.setSysCode("sysCode1");
body1.setSysName("sysName1");
list.add(body1);
MailBody body2 = new MailBody();
body2.setId("en");
body2.setSysCode("sysCode2");
body2.setSysName("sysName2");
list.add(body2);
Mail mail = new Mail();
mail.setFrom("123");
mail.setTo("45p6");
mail.setMailBody(list);
System.out.println(xstream.toXML(mail));
}
@Test
public void testXmlToBean(){
XStream xstream = new XStream();
XStream.setupDefaultSecurity(xstream);
xstream.autodetectAnnotations(true);
List<MailBody> list = new ArrayList<MailBody>();
MailBody body1 = new MailBody();
body1.setId("cn");
body1.setSysCode("sysCode1");
body1.setSysName("sysName1");
list.add(body1);
MailBody body2 = new MailBody();
body2.setId("en");
body2.setSysCode("sysCode2");
body2.setSysName("sysName2");
list.add(body2);
Mail mail = new Mail();
mail.setFrom("123");
mail.setTo("45p6");
mail.setMailBody(list);
String xml = xstream.toXML(mail);
Mail mail2 = (Mail)xstream.fromXML(xml);
System.out.println("mail2 :"+mail2.toString());
}
}