
最代码官方 LV168
2015年11月19日
响尾蛇 LV15
2015年11月20日
用XStream写个demo给大家看看:
maven依赖:
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>xpp3</groupId> <artifactId>xpp3</artifactId> <version>1.1.4c</version> </dependency>
三个类:
Student.java
package com.simpco.xstreamtest; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Student") public class Student { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student() { super(); } @Override public String toString() { return "Student [name=" + name + ", sex=" + sex + "]"; } }
Root.java
package com.simpco.xstreamtest; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Root") public class Root { @XStreamAlias("StudentList") private List<Student> studentList; public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } public void addStudent(Student student){ if(studentList==null){ studentList = new ArrayList<Student>(); } studentList.add(student); } }
运行 App.java
package com.simpco.xstreamtest; import com.thoughtworks.xstream.XStream; /** * Hello world! * */ public class App { public static void main( String[] args ){ XStream xstream = new XStream(); Root root = new Root(); Student student = new Student(); student.setName("张三"); student.setSex("男"); root.addStudent(student); Student student1 = new Student(); student1.setName("李四"); student1.setSex("女"); root.addStudent(student1); // xstream.alias("root", Root.class); // xstream.alias("Student", Student.class); // Annotations.configureAliases(xstream, Student.class); xstream.processAnnotations(Root.class); xstream.processAnnotations(Student.class);//声明使用Student中的注解别名 String xml = xstream.toXML(root); String top = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; System.out.println(top+xml); // Root rootObj = (Root)xstream.fromXML(xml); // System.out.println(JSON.toJSONString(rootObj)); } }
预期结果:
<?xml version="1.0" encoding="UTF-8"?> <Root> <StudentList> <Student> <name>张三</name> <sex>男</sex> </Student> <Student> <name>李四</name> <sex>女</sex> </Student> </StudentList> </Root>
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?


sp朱盼 LV16
2016年9月28日
笑谈一纸风 LV3
2016年9月19日
樱花满天 LV24
2016年2月23日
tangtang00 LV2
2015年12月29日
temptation LV16
2015年12月7日
leva LV1
2015年11月27日
lg906321 LV9
2015年11月26日
langmanxch LV3
2015年11月26日
surface LV4
2015年11月25日
小飞鸟 LV16
2015年11月23日