XStream入门介绍3
Java 到xml,用toXML()方法。
Xml到Java,用fromXML()方法。
在没有任何设置默认情况下,java到xml的映 射,是java成员名对应xml的元素名,java类的全名对应xml根元素的名字。而实际中,往往是xml和java类都有了,要完成相互转换,必须进行别名映射。
eg 1:
package com.leadtone.util.xstream;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;public class XstreamDemo {public static void main(String[] args) {XStream xstream = new XStream(new DomDriver());Person joe = new Person("Bill", "Gates");joe.setFaxNumber(new PhoneNumber(101, "83501194"));joe.setMobileNumber(new PhoneNumber(102, "13686447788"));String xml = xstream.toXML(joe);System.out.println(xml);}}
?输出结果:
?
<com.leadtone.util.xstream.Person> <firstName>Bill</firstName> <lastName>Gates</lastName> <faxNumber> <phoneId>101</phoneId> <phoneNumber>83501194</phoneNumber> </faxNumber> <mobileNumber> <phoneId>102</phoneId> <phoneNumber>13686447788</phoneNumber> </mobileNumber></com.leadtone.util.xstream.Person>
?【注】:这里的最外层标签是<com.leadtone.util.xstream.Person>,而不是<Person>
eg 2:
package com.leadtone.util.xstream;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;public class XstreamDemo {public static void main(String[] args) {XStream xstream = new XStream(new DomDriver());xstream.alias("person", Person.class);//映射Person joe = new Person("Bill", "Gates");joe.setFaxNumber(new PhoneNumber(101, "83501194"));joe.setMobileNumber(new PhoneNumber(102, "13686447788"));String xml = xstream.toXML(joe);System.out.println(xml);}}
?结果:
<person> <firstName>Bill</firstName> <lastName>Gates</lastName> <faxNumber> <phoneId>101</phoneId> <phoneNumber>83501194</phoneNumber> </faxNumber> <mobileNumber> <phoneId>102</phoneId> <phoneNumber>13686447788</phoneNumber> </mobileNumber></person>
?
别名配置包含三种情况:
1、类别名,用alias(String name, Class type)。
2、 类成员别名,用aliasField(String alias, Class definedIn, String fieldName)
3、 类成员作为属性别名,用 aliasAttribute(Class definedIn, String attributeName, String alias),单独命名没有意义,还要通过useAttributeFor(Class definedIn, String fieldName) 应用到某个类上。
eg 3:
package com.leadtone.util.xstream;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;public class XstreamDemo {public static void main(String[] args) {XStream xstream = new XStream(new DomDriver());xstream.alias("person", Person.class);xstream.aliasField("fn", Person.class,"firstName");xstream.aliasField("ln", Person.class,"lastName");Person joe = new Person("Bill", "Gates");joe.setFaxNumber(new PhoneNumber(101, "83501194"));joe.setMobileNumber(new PhoneNumber(102, "13686447788"));String xml = xstream.toXML(joe);System.out.println(xml);}}
?输出结果:
<person> <fn>Bill</fn> <ln>Gates</ln> <faxNumber> <phoneId>101</phoneId> <phoneNumber>83501194</phoneNumber> </faxNumber> <mobileNumber> <phoneId>102</phoneId> <phoneNumber>13686447788</phoneNumber> </mobileNumber></person>
?eg 4:
?
package com.leadtone.util.xstream;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;public class XstreamDemo {public static void main(String[] args) {XStream xstream = new XStream(new DomDriver());xstream.alias("person", Person.class);xstream.aliasField("fn", Person.class,"firstName");xstream.aliasField("ln", Person.class,"lastName");xstream.useAttributeFor(Person.class, "firstName"); //设置属性Person joe = new Person("Bill", "Gates");joe.setFaxNumber(new PhoneNumber(101, "83501194"));joe.setMobileNumber(new PhoneNumber(102, "13686447788"));String xml = xstream.toXML(joe);System.out.println(xml);}}
?运行结果:
<person fn="Bill"> <ln>Gates</ln> <faxNumber> <phoneId>101</phoneId> <phoneNumber>83501194</phoneNumber> </faxNumber> <mobileNumber> <phoneId>102</phoneId> <phoneNumber>13686447788</phoneNumber> </mobileNumber></person>
?
别名的配置是非常重要的,但是其中有些细节问题很重要,在例子中会专门做详细说明。
另外还有不太常用的方法:
addImplicitCollection(Class ownerType, String fieldName),去掉集合类型生成xml的父节点。
registerConverter(Converter converter) ,注册一个转换器。
?
?
?
?
eg 5:把对象中的map映射为xml
?
?
?
?
?