一、 XML与JSON的生成
示例一:将Java对象转换成XML数据
package com.ibeifeng.xstream; ? import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; ? public class TestXStream { ?????? public static void main(String[] args) { ????????????? Person person = new Person(); ????????????? person.setName("张三"); ????????????? person.setAge(20); ????????????? person.setSex(true); ????????????? DomDriver domDriver = new DomDriver(); ????????????? XStream xtream = new XStream(domDriver); ????????????? xtream.alias("person", Person.class); ????????????? String xml = xtream.toXML(person); ????????????? System.out.println(xml); ?????? } } |
示例二:将Java对象集合转换成XML数据
package com.ibeifeng.xstream; ? import java.util.ArrayList; import java.util.List; ? import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; ? public class TestXStreamList { ?????? public static void main(String[] args) { ????????????? List<Person> persons = new ArrayList<Person>(); ????????????? Person person1 = new Person(); ????????????? person1.setName("张三"); ????????????? person1.setAge(20); ????????????? person1.setSex(true); ????????????? ????????????? Person person2 = new Person(); ????????????? person2.setName("李四"); ????????????? person2.setAge(30); ????????????? person2.setSex(false); ????????????? ????????????? persons.add(person1); ????????????? persons.add(person2); ????????????? ????????????? DomDriver domDriver = new DomDriver(); ????????????? XStream xtream = new XStream(domDriver);
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|