xStream不仅对XML的转换非常友好,而且提供annotation注解,可以在JavaBean中完成对xml节点、属性的描述。以及对JSON也支持,只需要提供相关的JSONDriver就可以完成转换。
1 用Junit作为测试工具
//junit 测试类 public class XStreamTest { private XStream xstream = null; private ObjectOutputStream out = null; private ObjectInputStream in = null; private Student bean = null; /** * <b>function:</b>初始化资源准备 * @author hoojo * @createDate Nov 27, 2010 12:16:28 PM */ @Before public void init() { try { xstream = new XStream(); //xstream = new XStream(new DomDriver()); // 需要xpp3 jar } catch (Exception e) { e.printStackTrace(); } bean = new Student(); bean.setAddress("china"); bean.setEmail("jack@email.com"); bean.setId(1); bean.setName("jack"); Birthday day = new Birthday(); day.setBirthday("2010-11-22"); bean.setBirthday(day); } /** * <b>function:</b>释放对象资源 */ @After public void destory() { xstream = null; bean = null; try { if (out != null) { out.flush(); out.close(); } if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } System.gc(); } public final void fail(String string) { System.out.println(string); } public final void failRed(String string) { System.err.println(string); } }
需要的基础实体类
public class Birthday { private String birthday; }
package xstream; public class Student { private int id; private String name; private String email; private String address; private Birthday birthday; //getter、setter public String toString() { return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email; } }
二 测试java类转为xml方法
1 实体bean转换成XML
@Test public void writeBean2XML() { try { fail("------------Bean->XML------------"); fail(xstream.toXML(bean)); fail("重命名后的XML"); //类重命名 //xstream.alias("account", Student.class); //xstream.alias("生日", Birthday.class); //xstream.aliasField("生日", Student.class, "birthday"); //xstream.aliasField("生日", Birthday.class, "birthday"); //fail(xstream.toXML(bean)); //属性重命名 // xstream.aliasField("邮件", Student.class, "email"); //包重命名 // xstream.aliasPackage("xstream", "com.hoo.entity"); fail(xstream.toXML(bean)); } catch (Exception e) { e.printStackTrace(); } }
测试答应的xml文档 其中 第二个xml中的报名是被重命名的
------------Bean->XML------------ <xstream.Student> <id>1</id> <name>jack</name> <email>jack@email.com</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> </xstream.Student> 重命名后的XML <xstream__rename.Student> <id>1</id> <name>jack</name> <email>jack@email.com</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> </xstream__rename.Student>
2、 将List集合转换成xml文档
@Test public void writeList2XML() { try { //修改元素名称 xstream.alias("beans", ListBean.class); xstream.alias("student", Student.class); fail("----------List-->XML----------"); ListBean listBean = new ListBean(); listBean.setName("this is a List Collection"); List<Object> list = new ArrayList<Object>(); list.add(bean); list.add(bean);//引用bean 这个bean和第一个bean相同 //list.add(listBean);//引用listBean,父元素 bean = new Student(); bean.setAddress("china"); bean.setEmail("tom@125.com"); bean.setId(2); bean.setName("tom"); Birthday day = new Birthday("2010-11-22"); bean.setBirthday(day); list.add(bean); listBean.setList(list); //将ListBean中的集合设置空元素,即不显示集合元素标签 //xstream.addImplicitCollection(ListBean.class, "list"); //设置reference模型 //xstream.setMode(XStream.NO_REFERENCES);//不引用 xstream.setMode(XStream.ID_REFERENCES);//id引用 //xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);//绝对路径引用 //将name设置为父类(Student)的元素的属性 xstream.useAttributeFor(Student.class, "name"); xstream.useAttributeFor(Birthday.class, "birthday"); //修改属性的name xstream.aliasAttribute("姓名", "name"); xstream.aliasField("生日", Birthday.class, "birthday"); fail(xstream.toXML(listBean)); } catch (Exception e) { e.printStackTrace(); } }
输出的结果
----------List-->XML---------- <beans id="1"> <name>this is a List Collection</name> <list id="2"> <student id="3" 姓名="jack"> <id>1</id> <email>jack@email.com</email> <address>china</address> <birthday id="4" 生日="2010-11-22"/> </student> <student reference="3"/> <student id="5" 姓名="tom"> <id>2</id> <email>tom@125.com</email> <address>china</address> <birthday id="6" 生日="2010-11-22"/> </student> </list> </beans>
如果不加xstream.addImplicitCollection(ListBean.class, "list");
这个设置的话,会出现一个List节点包裹着Student节点元素。添加addImplicitCollection可以忽略这个list节点元素。那么上面的list节点就不存在,只会在beans元素中出现name、student这2个xml元素标签;
setMode是设置相同的对象的引用方式,如果设置XStream.NO_REFERENCES就是不引用,会输出2分相同的Student元素。如果是XStream.ID_REFERENCES会引用相同的那个对象的id属性,如果是XStream.XPATH_ABSOLUTE_REFERENCES引用,那么它将显示xpath路径。上面采用的id引用,<student reference="3"/>这个引用了id=3的那个student标签元素;
useAttributeFor是设置某个节点显示到父节点的属性中,也就是将指定class中的指定属性,在这个class元素节点的属性中显示。
如:<student><name>hoojo</name></student>
设置好后就是这样的结果:<student name=”hoojo”></student>
aliasAttribute是修改属性名称。
3 在JavaBean中添加Annotation注解进行重命名设置
@XStreamAlias("class") public class Classes { /* * 设置属性显示 */ @XStreamAsAttribute @XStreamAlias("名称") private String name; /* * 忽略 */ @XStreamOmitField private int number; @XStreamImplicit(itemFieldName = "Students") private List<Student> students; @SuppressWarnings("unused") @XStreamConverter(SingleValueCalendarConverter.class) private Calendar created = new GregorianCalendar(); public Classes() { } public Classes(String name, Student... stu) { this.name = name; this.students = Arrays.asList(stu); } //set get }
SingleValueCalendarConverter.java这个是一个类型转换器
import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class SingleValueCalendarConverter implements Converter { public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { Calendar calendar = (Calendar) source; writer.setValue(String.valueOf(calendar.getTime().getTime())); } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(new Date(Long.parseLong(reader.getValue()))); return calendar; } @SuppressWarnings("unchecked") public boolean canConvert(Class type) { return type.equals(GregorianCalendar.class); } }
测试用例代码
@Test public void writeList2XML4Annotation() { try { failRed("---------annotation Bean --> XML---------"); Student stu = new Student(); stu.setName("jack"); Classes c = new Classes("一班", bean, stu); c.setNumber(2); //对指定的类使用Annotation //xstream.processAnnotations(Classes.class); //启用Annotation //xstream.autodetectAnnotations(true); xstream.alias("student", Student.class); fail(xstream.toXML(c)); } catch (Exception e) { e.printStackTrace(); } }
当启用annotation或是对某个特定的类启用annotation时,上面的classes这个类才有效果。如果不启用annotation,运行后结果如下
---------annotation Bean --> XML--------- <xstream.Classes> <name>一班</name> <number>2</number> <students class="java.util.Arrays$ArrayList"> <a class="student-array"> <student> <id>1</id> <name>jack</name> <email>jack@email.com</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> </student> <student> <id>0</id> <name>jack</name> </student> </a> </students> <created> <time>1361861808921</time> <timezone>Asia/Shanghai</timezone> </created> </xstream.Classes>
当启用annotation后xstream.processAnnotations(Classes.class),结果如下:
---------annotation Bean --> XML--------- <class 名称="一班"> <Students> <id>1</id> <name>jack</name> <email>jack@email.com</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> </Students> <Students> <id>0</id> <name>jack</name> </Students> <created>1303292242937</created> </class>
4 Map 转换为XML
@Test public void writeMap2XML() { try { failRed("---------Map --> XML---------"); Map<String, Student> map = new HashMap<String, Student>(); map.put("No.1", bean);// put bean = new Student(); bean.setAddress("china"); bean.setEmail("tom@125.com"); bean.setId(2); bean.setName("tom"); Birthday day = new Birthday("2010-11-22"); bean.setBirthday(day); map.put("No.2", bean);// put bean = new Student(); bean.setName("jack"); map.put("No.3", bean);// put xstream.alias("student", Student.class); xstream.alias("key", String.class); xstream.useAttributeFor(Student.class, "id"); xstream.useAttributeFor("birthday", String.class); fail(xstream.toXML(map)); } catch (Exception e) { e.printStackTrace(); } }
5、 用OutStream输出流写XML
@Test public void writeXML4OutStream() { try { out = xstream.createObjectOutputStream(System.out); Student stu = new Student(); stu.setName("jack"); Classes c = new Classes("一班", bean, stu); c.setNumber(2); failRed("---------ObjectOutputStream # JavaObject--> XML---------"); out.writeObject(stu); out.writeObject(new Birthday("2010-05-33")); out.write(22);//byte out.writeBoolean(true); out.writeFloat(22.f); out.writeUTF("hello"); } catch (Exception e) { e.printStackTrace(); } }
使用输出流后,可以通过流对象完成xml的构建,即使没有JavaBean对象,你可以用流来构建一个复杂的xml文档,运行后结果如下:
---------ObjectOutputStream # JavaObject--> XML--------- <object-stream> <xstream.Student> <id>0</id> <name>jack</name> </xstream.Student> <xstream.Birthday> <birthday>2010-05-33</birthday> </xstream.Birthday> <byte>22</byte> <boolean>true</boolean> <float>22.0</float> <string>hello</string> </object-stream>
三、XML内容转换Java对象
1、 用InputStream将XML文档转换成java对象
@Test public void readXML4InputStream() { try { String s = "<object-stream><xstream.Student><id>0</id><name>jack</name>" + "</xstream.Student><xstream.Birthday><birthday>2010-05-33</birthday>" + "</xstream.Birthday><byte>22</byte><boolean>true</boolean><float>22.0</float>" + "<string>hello</string></object-stream>"; failRed("---------ObjectInputStream## XML --> javaObject---------"); StringReader reader = new StringReader(s); in = xstream.createObjectInputStream(reader); Student stu = (Student) in.readObject(); Birthday b = (Birthday) in.readObject(); byte i = in.readByte(); boolean bo = in.readBoolean(); float f = in.readFloat(); String str = in.readUTF(); System.out.println(stu); System.out.println(b); System.out.println(i); System.out.println(bo); System.out.println(f); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } }
读取后,转换的Java对象,结果如下
---------ObjectInputStream## XML --> javaObject--------- jack#0#null#null#null xstream.Birthday@1786e64 22 true 22.0 hello
四、XStream对JSON的支持
xStream对JSON也有非常好的支持,它提供了2个模型驱动。用这2个驱动可以完成Java对象到JSON的相互转换。使用JettisonMappedXmlDriver驱动,将Java对象转换成json,需要添加jettison.jar
1、 用JettisonMappedXmlDriver完成Java对象到JSON的转换
@Test public void writeEntity2JETTSON() { failRed("=======JettisonMappedXmlDriver===JavaObject >>>> JaonString========="); xstream = new XStream(new JettisonMappedXmlDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.alias("student", Student.class); fail(xstream.toXML(bean)); }
测试结果:
=======JettisonMappedXmlDriver===JavaObject >>>> JaonString========= {"student":{"id":1,"name":"jack","email":"jack@email.com","address":"china","birthday":[{},"2010-11-22"]}}
JSON的转换和XML的转换用法一样,只是创建XStream需要传递一个参数,这个参数就是xml到JSON映射转换的驱动。这里会涉及到两个驱动,分别是JettisonMappedXmlDriver、JsonHierarchicalStreamDriver。
2、 JsonHierarchicalStreamDriver完成Java对象到JSON的转换
@Test public void writeEntiry2JSON() { failRed("======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString========="); xstream = new XStream(new JsonHierarchicalStreamDriver()); //xstream.setMode(XStream.NO_REFERENCES); xstream.alias("student", Student.class); failRed("-------Object >>>> JSON---------"); fail(xstream.toXML(bean)); //failRed("========JsonHierarchicalStreamDriver==删除根节点========="); //删除根节点 xstream = new XStream(new JsonHierarchicalStreamDriver() { public HierarchicalStreamWriter createWriter(Writer out) { return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE); } }); //xstream.setMode(XStream.NO_REFERENCES); xstream.alias("student", Student.class); fail(xstream.toXML(bean)); }
运行结果
======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString========= -------Object >>>> JSON--------- {"student": { "id": 1, "name": "jack", "email": "jack@email.com", "address": "china", "birthday": { "birthday": "2010-11-22" } }} { "id": 1, "name": "jack", "email": "jack@email.com", "address": "china", "birthday": { "birthday": "2010-11-22" } }
使用JsonHierarchicalStreamDriver转换默认会给转换后的对象添加一个根节点,但是在构建JsonHierarchicalStreamDriver驱动的时候,你可以重写createWriter方法,删掉根节点。
看上面的结果,一个是默认带根节点的JSON对象,它只是将类名作为一个属性,将对象作为该属性的一个值。而另一个没有带根属性的JSON就是通过重写createWriter方法完成的。
@Test public void writeList2JSON() { failRed("======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString========="); JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver(); xstream = new XStream(driver); // xstream = new XStream(new JettisonMappedXmlDriver());//转换错误 // xstream.setMode(XStream.NO_REFERENCES); xstream.alias("student", Student.class); List<Student> list = new ArrayList<Student>(); list.add(bean);// add bean = new Student(); bean.setAddress("china"); bean.setEmail("tom@125.com"); bean.setId(2); bean.setName("tom"); Birthday day = new Birthday("2010-11-22"); bean.setBirthday(day); list.add(bean);// add bean = new Student(); bean.setName("jack"); list.add(bean);// add fail(xstream.toXML(list)); // failRed("========JsonHierarchicalStreamDriver==删除根节点========="); // 删除根节点 xstream = new XStream(new JsonHierarchicalStreamDriver() { public HierarchicalStreamWriter createWriter(Writer out) { return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE); } }); xstream.alias("student", Student.class); fail(xstream.toXML(list)); }
上面的list1是使用JsonHierarchicalStreamDriver 转换的,当然你也可以使用JettisonMappedXmlDriver驱动进行转换;用JettisonMappedXmlDriver转换后,你会发现格式不同而且没有根属性。
4、 Map转换json
@Test public void writeMap2JSON() { failRed("======JsonHierarchicalStreamDriver==== Map >>>> JaonString========="); xstream = new XStream(new JsonHierarchicalStreamDriver()); // xstream = new XStream(new JettisonMappedXmlDriver()); xstream.alias("student", Student.class); Map<String, Student> map = new HashMap<String, Student>(); map.put("No.1", bean);// put bean = new Student(); bean.setAddress("china"); bean.setEmail("tom@125.com"); bean.setId(2); bean.setName("tom"); bean.setBirthday(new Birthday("2010-11-21")); map.put("No.2", bean);// put bean = new Student(); bean.setName("jack"); map.put("No.3", bean);// put fail(xstream.toXML(map)); // failRed("========JsonHierarchicalStreamDriver==删除根节点========="); // 删除根节点 xstream = new XStream(new JsonHierarchicalStreamDriver() { public HierarchicalStreamWriter createWriter(Writer out) { return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE); } }); xstream.alias("student", Student.class); fail(xstream.toXML(map)); }
相关推荐
3. **初始化xStream**:创建xStream实例并进行配置,以指定如何将Java对象转换为XML。 ```java XStream xstream = new XStream(); xstream.alias("person", Person.class); // "person"是XML中的标签名 ``` 4. ...
以下是一个简单的示例,演示如何使用XStream进行XML与Java对象的转换: ```java import com.thoughtworks.xstream.XStream; class User { private String name; private int age; // getters and setters ...
要将这个Java对象转换为XML,可以这样做: ```java import com.thoughtworks.xstream.XStream; // 创建XStream实例 XStream xstream = new XStream(); // 配置XStream,使其忽略默认的无参构造函数 xstream....
XStream的核心在于`toXML()`方法,它可以将任何Java对象转换成XML字符串。例如,如果你有一个简单的User类,包含name和age属性,你可以这样转换: ```java User user = new User("Alice", 25); XStream xstream...
首先创建XStream实例,然后使用`toXML()`方法将Java对象转换为XML字符串: ```java XStream xstream = new XStream(); User user = new User(); user.setName("Forest"); user.setAge(30); String xml = xstream.to...
总结起来,XStream是一个强大的工具,可以方便地进行Java对象与XML之间的转换。通过理解其基本原理和使用方法,开发者可以在处理数据交换和持久化问题时,节省大量时间和精力。在实践中,结合源码分析和工具使用,...
有了配置好的xStream实例,你可以将Java对象转换为XML字符串: ```java Employee employee = new Employee("John", "Doe"); String xml = xstream.toXML(employee); System.out.println(xml); ``` 4. **反...
XStream是一个强大的库,专门用于XML与Java对象之间的互相转换。这篇博文将深入探讨如何利用XStream的转换器功能来优化这一过程。 XStream的核心原理是通过反射机制自动将Java对象映射到XML结构,并反之亦然。然而...
首先,创建一个XStream实例,然后使用`toXML()`方法将Java对象转换为XML字符串: ```java XStream xstream = new XStream(); User user = new User(); user.setName("张三"); user.setAge(30); String xml = ...
在“xstream使用3”这个压缩包文件中,可能包含了使用XStream注解实现XML与Java对象转换的示例代码、测试用例以及相关的说明文档。通过对这些文件的深入学习,开发者可以更好地理解如何在实际项目中运用XStream的...
在这个版本中,XStream提供了对Java Bean对象转换为XML的全面支持。 **XStream的工作原理:** XStream的核心是基于反射的,它通过分析Java对象的类结构来构建XML表示。它使用了一种称为"alias"的概念,允许我们将...
XStream是基于反射和类型安全的转换器,它通过将Java对象转换为XML流,实现了对象的序列化,反之亦然。它的主要优点在于其简洁的API和可定制性,允许开发者以声明式的方式定义对象和XML之间的映射关系。 在持久化...
它可以将Java对象转换为XML文档,也可以将XML文档转换为Java对象。 pom引用 <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream <version>1.4.20 实体类和测试类详见博客 ...
然后,我们可以将一个Person对象转换为XML: ```java Person person = new Person(); person.setName("John"); person.setAge(30); String xml = xstream.toXML(person); System.out.println(xml); ``` 输出的XML...
然后,将Java对象转换为XML字符串: ```java Person person = new Person(); person.setName("John"); person.setAge(30); String xml = xstream.toXML(person); ``` 现在,我们有了XML表示的`Person`对象。为了...
XStream 的核心概念是通过自动映射 Java 类和 XML 节点来实现对象和 XML 的转换。它通过反射和自定义转换器机制来完成这一过程。默认情况下,XStream 可以处理大多数标准的 Java 类型,包括基本类型、集合、Map 等。...
接着,你可以将Java对象转换为XML字符串: ```java Person person = new Person(); person.setName("John"); person.setAge(30); String xml = xstream.toXML(person); System.out.println(xml); ``` 反之,如果...