import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;
public class Test {
/**
* 目标对象
* @author Lihc
*
*/
class UserTest {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public static void main(String[] args)throws Exception{
/*
* 准备测试数据
*/
List u = new ArrayList();
for(int i = 0; i<2;i++){
UserTest r = new UserTest();
r.setName("测试");
r.setAge(100);
u.add(r);
}
/*
* 测试过程
*/
XStream xstream = new XStream(new Dom4JDriver());
xstream.alias("user", UserTest.class);
Document document = DocumentHelper.createDocument(DocumentHelper.createElement("yycs"));
Document document1 = DocumentHelper.parseText(xstream.toXML(u));
Document document2 = DocumentHelper.parseText(xstream.toXML(u));
Element user1 = (Element)document1.selectSingleNode("//list");
//Element user2 = (Element)document2.selectSingleNode("//list");
document.getRootElement().appendContent(user1);
// document.getRootElement().appendContent(user2);
System.out.println(document.asXML());
}
}
java对象转换成xml
最新推荐文章于 2021-02-27 23:50:59 发布