注意事项:
创建接收转换的实体类 里面字段 与 xml节点一一对应。spring 使用 @XStreamAlias("节点") 进行关联。属性名与节点一一对应是很重要的!!!不然 不能正常转换。使用了注解以后,xml节点跟类的属性名不再有关系,但是 注解里面使用的名字就默认是与xml节点对应的。所以保持@XStreamAlias("节点") 里面节点的值与xml 一一对应。
xstream.processAnnotations(需要转化的实体类.class);
实体类 例子:
package com.cmsz.cmup.alipay.model.responses;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
/**
* @Title: AccountResultVO.java
* @Package com.cmsz.cmup.alipay.vo.result
* @Description: alipay返回xml字符串封装对象
* @author yekun
* @date 2015年12月4日 上午10:50:23
*/
@XStreamAlias("alipay")
public class AccountResponseModel {
/** 错误信息 */
@XStreamAlias("error")
private String error;
/** 请求是否成功 */
@XStreamAlias("is_success")
private String isSuccess;
/** 请求参数 */
@XStreamAlias("request")
private Request request;
/** xml节点 */
@XStreamAlias("response")
private Response response;
/** 签名 */
@XStreamAlias("sign")
private String sign;
/** 签名加密方式 */
@XStreamAlias("sign_type")
private String sign_type;
//转换代码
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(实体类.class);
xstream.fromXML(xmlStr);//xml格式的字符串
Object object = xstream.fromXML(xmlString);
static MessageDatabase load(String persistFile) {
MessageDatabase messageDatabase = null;
Reader reader = null;
try {
reader = new FileReader(persistFile);
XStream xstream = new XStream(new JDomDriver());
messageDatabase = (MessageDatabase) xstream.fromXML(reader);
// for upgrade
if (messageDatabase != null) {
messageDatabase.upgrade();
}
}
catch (FileNotFoundException ex) {
;
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException ex) {
log.warn("Exception when closing reader", ex);
}
}
}
if (messageDatabase == null) {
messageDatabase = new MessageDatabase();
messageDatabase.setMessages(new LinkedList());
messageDatabase.setStickyMessages(new LinkedList());
}
messageDatabase.persistFile = persistFile;
return messageDatabase;
}
2014-05-17
UnmarshallingContext context) {
Map<String, Object> map = new HashMap<String,Object>();
populateMap(reader,context,map);
return map;
}
protected void populateMap(HierarchicalStreamReader reader,
UnmarshallingContext context, Map<String, Object> map) {
while (reader.hasMoreChildren()) {
reader.moveDown();
String key = reader.getNodeName();
Object value = null;
if (reader.hasMoreChildren()) {
Map<String, Object> innerNode = new HashMap<String, Object>();
populateMap(reader, context, innerNode);
value = innerNode;
} else {
value = reader.getValue();
}
if (map.containsKey(key)) {
// convert to list
ArrayList<Object> list = null;
if(map.get(key) instanceof ArrayList){
list = (ArrayList<Object>)map.get(key);
}else{
list = new ArrayList<Object>();
list.add(map.get(key));
}
list.add(value);
map.put(key, list);
} else {
// insert into map
map.put(key, value);
}
reader.moveUp();
}
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XstreamTest {
public static void main(String[] args) {
//多年前用过,既然问了 我也温故知新一下
//x-stream的下载地址 http://x-stream.github.io/download.html
//将user对象转换为xml
User u = new User();
u.setAge(18);
u.setName("小明");
XStream xs = new XStream(new DomDriver());
String userXml = xs.toXML(u);
System.out.println(userXml);
//将xml转换为user对象
User user = (User) xs.fromXML("<User><name>小张</name><age>22</age></User>");
System.out.println(user.getName());
System.out.println(user.getAge());
}
}
class User {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}