----------List--
>XML----------
<beans id="1">this is a List Collection<list id="2"<student id="3" 姓名="jack" <birthday id="4" 生日="2010-11-22"/</student<student reference="3"/<student id="5" 姓名="tom">2>tom@125<birthday id="6" 生日="2010-11-22"/</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注解进行重命名设置
先看看JavaBean的代码
@H_869_301@import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
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.annotations.XStreamOmitField;
@XStreamAlias("class")
class Classes {
/*
* 设置属性显示
@XStreamAsAttribute
@XStreamAlias("名称")
* 忽略
@XStreamOmitField
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);
SingleValueCalendarConverter.java这个是一个类型转换器
@H_869_301@import java.util.Date;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
class SingleValueCalendarConverter implements Converter {
void marshal(Object source,HierarchicalStreamWriter writer,
MarshallingContext context) {
Calendar calendar = (Calendar) source;
writer.setValue(String.valueOf(calendar.getTime().getTime()));
public Object unmarshal(HierarchicalStreamReader reader,244)"> UnmarshallingContext context) {
GregorianCalendar calendar = calendar.setTime(new Date(Long.parseLong(reader.getValue())));
return calendar;
}
@SuppressWarnings(boolean canConvert(Class type) {
return type.equals(GregorianCalendar. 再看看测试用例代码
@Test
void writeList2XML4Annotation() {
failRed("---------annotation Bean --> XML---------");
Student stu = stu.setName( Classes c = new Classes("一班",bean,stu);
c.setNumber(2);
//对指定的类使用Annotation
//xstream.processAnnotations(Classes.class);
//启用Annotation
//xstream.autodetectAnnotations(true);
fail(xstream.toXML(c));
当启用annotation或是对某个特定的类启用annotation时,上面的classes这个类才有效果。如果不启用annotation,运行后结果如下:
---------annotation Bean --
> XML---------
.Classes>一班<number</number<students class="java.util.Arrays$ArrayList"<a class="student-array"<student >0</a</students<created<time>1303292056718</time<timezone>Asia/Shanghai</timezone</created 当启用annotation后xstream.processAnnotations(Classes.class),结果如下:
<class
名称="一班"
<Students
</Students
>1303292242937
</class
4、 Map集合转换xml文档
* <b>function:</b>Java Map集合转XML
void writeMap2XML() {
failRed("---------Map --> XML---------");
Map<String,Student> map = new HashMap<String,Student>();
map.put("No.1",bean);//put
map.put("No.2",128)">"No.3",128)">"key",String. xstream.useAttributeFor(Student."id");
xstream.useAttributeFor("birthday",244)"> fail(xstream.toXML(map));
---------Map --map<entry<key>No.3</key<student id="0"</entry>No.1<student id="1"<birthday birthday="2010-11-22"/>No.2<student id="2">tom 5、 用OutStream输出流写XML
* <b>function:</b>用OutStream
输出流写XML
void writeXML4OutStream() {
out = xstream.createObjectOutputStream(System.out);
Student stu = stu.setName( Classes c = c.setNumber(2);
"---------ObjectOutputStream # JavaObject--> XML---------");
out.writeObject(stu);
out.writeObject("2010-05-33"));
out.write(22);//byte
out.writeBoolean(true);
out.writeFloat(22.f);
out.writeUTF("hello");
使用输出流后,可以通过流对象完成xml的构建,即使没有JavaBean对象,你可以用流来构建一个复杂的xml文档,运行后结果如下:
---------ObjectOutputStream # JavaObject--
object-stream
.Birthday>2010-05-33
<byte
>22
</byte
<boolean
>true
</boolean
float>22.0
<string
>hello
</string
三、XML内容转换Java对象
1、 用InputStream将XML文档转换成java对象
* <b>function:</b>用InputStream将XML文档转换成java对象
* 需要额外的jar xpp3-main.jar
void readXML4InputStream() {
String s = "<object-stream><com.hoo.entity.Student><id>0</id><name>jack</name>" +
"</com.hoo.entity.Student><com.hoo.entity.Birthday><birthday>2010-05-33</birthday>" +
"</com.hoo.entity.Birthday><byte>22</byte><boolean>true</boolean><float>22.0</float>" +
"<string>hello</string></object-stream>";
"---------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);
读取后,转换的Java对象,结果如下:
---------ObjectInputStream## XML --> javaObject---------
jack#0#null#null#null
2010-05-33
22
true
22.0
hello
2、 将xml文档转换成Java对象
* <b>function:</b>将XML字符串转换成Java对象
void readXml2Object() {
"-----------Xml >>> Bean--------------");
Student stu = (Student) xstream.fromXML(xstream.toXML(bean));
fail(stu.toString());
List<Student> list = new ArrayList<Student>();
//add
list.add(bean);"==========XML >>> List===========");
List<Student> studetns = (List<Student>) xstream.fromXML(xstream.toXML(list));
"size:" + studetns.size());//3
for (Student s : studetns) {
fail(s.toString());
"==========XML >>> Map===========");
"size:" + maps.size()); Set<String> key = maps.keySet();
Iterator<String> iter = key.iterator();
while (iter.hasNext()) {
String k = iter.next();
fail(k + ":" + map.get(k));
-----------Xml >>> Bean--------------
jack#1#china#2010-11-22#jack@email.com
==========XML >>> List===========
size:3
jack#1#china#2010-11-22#jack@email.com
tom#2#china#2010-11-22#tom@125.com
jack#0#null#null#null
==========XML >>> Map===========
size:3
No.3:jack#0#null#null#null
No.1:jack#1#china#2010-11-22#jack@email.com
No.2:tom#2#china#2010-11-22#tom@125.com
怎么样,成功的完成XML到JavaBean、List、Map的转换,更多对象转换还需要大家一一尝试。用法类似~这里就不一样赘述。
四、XStream对JSON的支持
xStream对JSON也有非常好的支持,它提供了2个模型驱动。用这2个驱动可以完成Java对象到JSON的相互转换。使用JettisonMappedXmlDriver驱动,将Java对象转换成json,需要添加jettison.jar
1、 用JettisonMappedXmlDriver完成Java对象到JSON的转换
* <b>function:</b>XStream结合JettisonMappedXmlDriver驱动,转换Java对象到JSON
* 需要添加jettison jar
void writeEntity2JETTSON() {
failRed("=======JettisonMappedXmlDriver===JavaObject >>>> JaonString=========");
xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias( fail(xstream.toXML(bean));
=======JettisonMappedXmlDriver===JavaObject >>>> JaonString=========
{"student":{"id":1,128)">"name":"jack",128)">"email":"jack@email.com",128)">"address":"china",128)">"birthday":[{},128)">"2010-11-22"]}}
JSON的转换和XML的转换用法一样,只是创建XStream需要传递一个参数,这个参数就是xml到JSON映射转换的驱动。这里会降到两个驱动,分别是JettisonMappedXmlDriver、JsonHierarchicalStreamDriver。
2、 JsonHierarchicalStreamDriver完成Java对象到JSON的转换
* <b>function:</b>用XStream结合JsonHierarchicalStreamDriver驱动
* 转换java对象为JSON字符串
void writeEntiry2JSON() {
"======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========");
new JsonHierarchicalStreamDriver());
//xstream.setMode(XStream.NO_REFERENCES);
"-------Object >>>> JSON---------");
fail(xstream.toXML(bean));
//failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
//删除根节点
new JsonHierarchicalStreamDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
new JsonWriter(out,JsonWriter.DROP_ROOT_MODE);
});
======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========
-------Object >>>> JSON---------
{"student": {
"id": 1,128)">"name": "email": "address": "birthday": {
"birthday": "2010-11-22"
}
}}
{
}
使用JsonHierarchicalStreamDriver转换默认会给转换后的对象添加一个根节点,但是在构建JsonHierarchicalStreamDriver驱动的时候,你可以重写createWriter方法,删掉根节点。
看上面的结果,一个是默认带根节点的JSON对象,它只是将类名作为一个属性,将对象作为该属性的一个值。而另一个没有带根属性的JSON就是通过重写createWriter方法完成的。
3、 将List集合转换成JSON字符串
@H_869_301@void writeList2JSON() {
JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver();
xstream = new XStream(driver);
//xstream = new XStream(new JettisonMappedXmlDriver());//转换错误
List<Student> list = list.add(bean); bean = bean.setAddress( bean.setEmail( bean.setId(2);
bean.setName( Birthday day = bean.setBirthday(day);
list.add(bean); bean = fail(xstream.toXML(list));
xstream.alias( 运行后结果如下
##{
"list": [
{
},244)"> {
"id": 2,128)">"tom",128)">"tom@125.com",244)"> },128)">"id": 0,128)">"jack"
]}
#[
]
上面的list1是使用JsonHierarchicalStreamDriver 转换的,当然你也可以使用JettisonMappedXmlDriver驱动进行转换;用JettisonMappedXmlDriver转换后,你会发现格式不同而且没有根属性。
4、 Map转换json
@H_869_301@void writeMap2JSON() {
"======JsonHierarchicalStreamDriver==== Map >>>> JaonString=========");
//xstream = new XStream(new JettisonMappedXmlDriver());
Map<String,monospace; direction:ltr; font-size:10pt; overflow:visible; background-color:white"> map.put( bean.setBirthday("2010-11-21"));
bean.setName( fail(xstream.toXML(map));
});
======JsonHierarchicalStreamDriver==== Map >>>> JaonString=========
"map": [
[
{
],244)"> [
{
}
"2010-11-21"
]
]}
[
],244)"> }
]
5、 将JSON转换java对象
* <b>function:</b>JsonHierarchicalStreamDriver可以将简单的json字符串转换成java对象,list、map转换不成功;
* JsonHierarchicalStreamDriver读取JSON字符串到java对象出错
* @throws JSONException
void readJSON2Object() throws JSONException {
String json = "{\"student\": {" +
"\"id\": 1," +
"\"name\": \"haha\",128)">"\"email\": \"email\",128)">"\"address\": \"address\",128)">"\"birthday\": {" +
"\"birthday\": \"2010-11-22\"" +
"}" +
"}}";
//JsonHierarchicalStreamDriver读取JSON字符串到java对象出错,但JettisonMappedXmlDriver可以
fail(xstream.fromXML(json).toString());
//JettisonMappedXmlDriver转换List集合出错,但JsonHierarchicalStreamDriver可以转换正确
//JettisonMappedXmlDriver 转换的字符串 {"list":{"student":[{"id":1,"name":"haha","email":"email","address":"address","birthday":[{},"2010-11-22"]}]},"student":{"id":2,"name":"tom","email":"tom@125.com","address":"china","2010-11-22"]}}
json = "{\"list\": [{" +
"},{" +
"\"id\": 2,128)">"\"name\": \"tom\",128)">"\"email\": \"tom@125.com\",128)">"\"address\": \"china\",128)">"}]}";
System.out.println(json);//用js转换成功
List list = (List) xstream.fromXML(json);
System.out.println(list.size());//0好像转换失败
haha#1#address#2010-11-22#email
"list": [{"haha",128)">"email",128)">"address",128)">"birthday": {"2010-11-22"}},128)">"2010-11-22"}}]}
0
JSON到Java的转换是fromXML方法。
本文转自:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
原文链接:https://www.f2er.com/xml/298203.html