xstream文章转载

<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com">
<class name="Person" table="novem">
<id name="id" column="ID" type="integer">
<generator class="increment"/>
</id>
<property name="name" column="NAME"/>
<property name="email" column="EMAIL"/>
</class>
</hibernate-mapping>

import java.io.BufferedWriter;   
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class createHbmXmlTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
XStream xstream = new XStream(new DomDriver());
try{
File write = new File("d:\\Data\\Person.hbm.xml");
BufferedWriter brWriter= new BufferedWriter(new FileWriter(write));

String xmlHead="\n\n";
String xmlBody;
// give the class or property an alias
xstream.alias("hibernate-mapping", HibernateMapping.class);
xstream.alias("class", Class.class);
xstream.alias("property", Property.class);
xstream.aliasAttribute("class","generatorClass");
xstream.aliasAttribute("package", "tpackage");
xstream.aliasField("class", HibernateMapping.class, "tclass");
// hiden properties
xstream.addImplicitCollection(Class.class,"properties");
xstream.addImplicitCollection(Generator.class, "generatorClass");

List proList=new ArrayList ();
Property pro1=new Property();
pro1.setName("name");
pro1.setColumn("NAME");
Property pro2=new Property();
pro2.setName("email");
pro2.setColumn("EMAIL");
proList.add(pro1);
proList.add(pro2);

Generator gen = new Generator("increment");
ID id=new ID("id","ID","integer",gen);

Class hClass=new Class("Person","novem");
hClass.setId(id);
hClass.setProperties(proList);
//set Package name add class
HibernateMapping hMapping= new HibernateMapping("com",hClass);

//take as Attribute
xstream.useAttributeFor(ID.class,"name");
xstream.useAttributeFor(ID.class, "column");
xstream.useAttributeFor(ID.class, "type");
xstream.useAttributeFor(Class.class, "name");
xstream.useAttributeFor(Class.class, "table");
xstream.useAttributeFor(Property.class, "name");
xstream.useAttributeFor(Property.class, "column");
xstream.useAttributeFor(Generator.class, "generatorClass");
xstream.useAttributeFor(HibernateMapping.class, "tpackage");
xstream.registerConverter(new PropertyConverter());

xmlBody=xstream.toXML(hMapping);
System.out.println(xmlHead+xmlBody);

brWriter.write(xmlHead+xmlBody);
brWriter.close();
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
e.printStackTrace();
}
}

}

XStream定制转换
XStream在实现Java、xml之间转换非常的出色。但也有一些问题需要解决:Java到XML,默认转换过去就使用Java成员属性的名字作为xml Element名,反过来亦如此。但这些都是不是实际中我们所要的结果。

为此,我又研读了XStream的文档,终于解决了这个问题,下面是测试代码。

package test;

import java.util.ArrayList;
import java.util.List;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-5-21 10:48:43<br>
* <b>Note</b>: 主体信息
*/
public class MainBody {
private int id;
private String name;
private List<Investor> investorList = new ArrayList<Investor>();

public MainBody(int id, String name, List<Investor> investorList) {
this.id = id;
this.name = name;
this.investorList = investorList;
}


public String toString() {
return "MainBody{" +
"id=" + id +
", name='" + name + '\'' +
", investorList=" + investorList +
'}';
}
}

package test;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-5-21 10:51:08<br>
* <b>Note</b>: 投资人
*/
public class Investor {
private int id;
private String name;
private int age;

public Investor(int id, String name) {
this.id = id;
this.name = name;
}

public Investor(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}

public String toString() {
return "Investor{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}

package test;

import com.thoughtworks.xstream.XStream;

import java.util.List;
import java.util.ArrayList;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-5-21 10:54:05<br>
* <b>Note</b>: 测试
*/
public class TestConvert {

public static void main(String args[]) {
test();
}

public static void test() {
System.out.println("----------test()----------");
Investor investor1 = new Investor(1, "gaici");
Investor investor2 = new Investor(2, "hahhah", 33);
List<Investor> investorList = new ArrayList<Investor>();
investorList.add(investor1);
investorList.add(investor2);
MainBody mainBody = new MainBody(1000, "微软", investorList);

XStream xStream = new XStream();

/************** 别名定义 Object -> xml ***************/

//没有做任何别名定义直接转换
outXML(1, xStream, mainBody);

//类别名
xStream.alias("MainBody", test.MainBody.class);
xStream.alias("Investor", test.Investor.class);
outXML(2, xStream, mainBody);

//列表节点别名
xStream.aliasField("Investor-List", MainBody.class, "investorList");
outXML(3, xStream, mainBody);

//一般属性节点别名
xStream.aliasField("ztbs", test.MainBody.class, "id");
xStream.aliasField("gdbs", test.Investor.class, "id");
outXML(4, xStream, mainBody);

//将name成员作为属性添加到Investor对应xml节点里
// xStream.aliasAttribute(Investor.class,"name","GDXM");
xStream.useAttributeFor(Investor.class, "name");
outXML(5, xStream, mainBody);

xStream.aliasAttribute(Investor.class, "age", "NL");
xStream.useAttributeFor(Investor.class, "age");
outXML(6, xStream, mainBody);

/***********************xml -> Object*************************/
String newxml = "<MainBody>\n" +
" <ztbs>1000</ztbs>\n" +
" <name>微软</name>\n" +
" <Investor-List>\n" +
" <Investor name=\"gaici\" NL=\"0\">\n" +
" <gdbs>1</gdbs>\n" +
" </Investor>\n" +
" <Investor name=\"hahhah\" NL=\"33\">\n" +
" <gdbs>2</gdbs>\n" +
" </Investor>\n" +
" </Investor-List>\n" +
"</MainBody>";
MainBody m = (MainBody) xStream.fromXML(newxml);
System.out.println("将最后一次所得的xml结果转换为Java对象输出:");
System.out.println(m);
}

public static void outXML(int index, XStream xStream, MainBody m) {
String xml = xStream.toXML(m);
System.out.println(">>>>>>>>>>第" + index + "次输出XML:");
System.out.println(xml + "\n");
}
}

运行结果:
----------test()----------
>>>>>>>>>>第1次输出XML:
<test.MainBody>
<id>1000</id>
<name>微软</name>
<investorList>
<test.Investor>
<id>1</id>
<name>gaici</name>
<age>0</age>
</test.Investor>
<test.Investor>
<id>2</id>
<name>hahhah</name>
<age>33</age>
</test.Investor>
</investorList>
</test.MainBody>

>>>>>>>>>>第2次输出XML:
<MainBody>
<id>1000</id>
<name>微软</name>
<investorList>
<Investor>
<id>1</id>
<name>gaici</name>
<age>0</age>
</Investor>
<Investor>
<id>2</id>
<name>hahhah</name>
<age>33</age>
</Investor>
</investorList>
</MainBody>

>>>>>>>>>>第3次输出XML:
<MainBody>
<id>1000</id>
<name>微软</name>
<Investor-List>
<Investor>
<id>1</id>
<name>gaici</name>
<age>0</age>
</Investor>
<Investor>
<id>2</id>
<name>hahhah</name>
<age>33</age>
</Investor>
</Investor-List>
</MainBody>

>>>>>>>>>>第4次输出XML:
<MainBody>
<ztbs>1000</ztbs>
<name>微软</name>
<Investor-List>
<Investor>
<gdbs>1</gdbs>
<name>gaici</name>
<age>0</age>
</Investor>
<Investor>
<gdbs>2</gdbs>
<name>hahhah</name>
<age>33</age>
</Investor>
</Investor-List>
</MainBody>

>>>>>>>>>>第5次输出XML:
<MainBody>
<ztbs>1000</ztbs>
<name>微软</name>
<Investor-List>
<Investor name="gaici">
<gdbs>1</gdbs>
<age>0</age>
</Investor>
<Investor name="hahhah">
<gdbs>2</gdbs>
<age>33</age>
</Investor>
</Investor-List>
</MainBody>

>>>>>>>>>>第6次输出XML:
<MainBody>
<ztbs>1000</ztbs>
<name>微软</name>
<Investor-List>
<Investor name="gaici" NL="0">
<gdbs>1</gdbs>
</Investor>
<Investor name="hahhah" NL="33">
<gdbs>2</gdbs>
</Investor>
</Investor-List>
</MainBody>

将最后一次所得的xml结果转换为Java对象输出:
MainBody{id=1000, name='微软', investorList=[Investor{id=1, name='gaici', age='0'}, Investor{id=2, name='hahhah', age='33'}]}

Process finished with exit code 0

还有一个测试:


运行结果:
----------test()----------
MainBody{id=1000, name='微软', investorList=[Investor{id=1, name='gaici', age='0'}, Investor{id=2, name='hahhah', age='0'}]}
Investor{id=1, name='gaici', age='0'}
Investor{id=2, name='hahhah', age='0'}
Investor{id=2, name='hahhah', age='0'}

Process finished with exit code 0



说明:
1、XStream不要求Java类的属性必须有getter、setter方法,因此省略。

2、设置java成员属性别名
xStream.aliasField("Investor-List",MainBody.class,"investorList");
outXML(3,xStream,mainBody);
运行结果会产生:
<Investor-List>

3、将java成员映射为xml元素的一个属性

//将name成员作为属性添加到Investor对应xml节点里
xStream.useAttributeFor(Investor.class,"name");

运行结果会产生:
<Investor name="hahhah">


下面这两句是相关联的:
xStream.aliasAttribute(Investor.class,"name","GDXM");
xStream.useAttributeFor(Investor.class,"name");
意思是先为java成员定义个别名,然后在将别名应用于xml属性上。

运行结果会产生:
<Investor GDXM="hahhah">

这些问题虽然解决了,但又发现了新的问题:xml转java时,当java中没有定义xml元素节点时,这时候会抛出异常。也许通过XStream本身的API可以解决,也许是XStream本身所不能处理的问题,时间有限,也没来得及深究。有知道的朋友,还望留言。

4、再进行xml到java 的转换过程中,XStream对象别名可以定义很多,涵盖的类的范围超过xml所能表达的范围,这个也没有关系,XStream还是会忠实地将xml还原为对象。但是如果xml范围大于XStream所涵盖类的范围,那么转换过程会出错。比如,要将一个Investor节点转换为ManiBody对象,肯定会出错。


存在的问题
设想我们的客户端定义了一个用于XStream读写的XML文件:
我们将设计一些模型类并配置XStream按照这个XML文件格式执行读写操作。

<blog author="Guilherme Silveira">
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</blog>
2,模型:
首先,建立一个简单的Blog对象:

package com.thoughtworks.xstream;
public class Blog {
private Author author;
private List entries = new ArrayList();
public Blog(Author author) {
this.author = author;
}
public void add(Entry entry) {
entries.add(entry);
}
public List getContent() {
return entries;
}
}

然后是一个带有名字的作者对象:

package com.thoughtworks.xstream;
public class Author {
private String name;
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
具体的blog内容对象:

package com.thoughtworks.xstream;
public class Entry {
private String title, description;
public Entry(String title, String description) {
this.title = title;
this.description = description;
}
}

虽然我们没有创建getters/setters方法,但这并不影响XStream对XML->Object文件的解析。
3,简单的测试
首先初始化一个blog实例,然后使用XStream来序列化

public static void main(String[] args) {
Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
teamBlog.add(new Entry("tutorial",
"Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
XStream xstream = new XStream();
System.out.println(xstream.toXML(teamBlog));
}

由该Blog实例解析出的XML文件为:

<com.thoughtworks.xstream.Blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entries>
<com.thoughtworks.xstream.Entry>
<title>first</title>
<description>My first blog entry.</description>
</com.thoughtworks.xstream.Entry>
<com.thoughtworks.xstream.Entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</com.thoughtworks.xstream.Entry>
</entries>
</com.thoughtworks.xstream.Blog>
4,为类取别名
首先我们来改变XStream对com.thoughtworks.xstream.Blog的输出名称。我们只想使用一个简单的blog来取代。下面为Blog类创建一个别名:
Xstream.alias("blog",Blog.class);
同样的,为Entry类创建一个别名:
Xstream.alias("entry",Entry.class);
好,到此输出的XML变为:
<blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entries>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</entries>
</blog>


5,去掉entries标记

下面,我们将实施叫做"implicit collection"的过程(即取消标记):所有的集合类型,都不需要显示他的根标签(root tag),你可以直接使用一个implicit collection去映射。

在我们的例子里面,我们不希望出现entries标签,只需要一个接一个的列出所有的entry标签即可。

要做到这点,只需要简单的调用XStream对象上的addImplicitCollection方法,就可以配置XStream取消对entries的输出:

package com.thoughtworks.xstream;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
teamBlog.add(new Entry("tutorial",
"Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
XStream xstream = new XStream();
xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);
xstream.addImplicitCollection(Blog.class, "entries");
System.out.println(xstream.toXML(teamBlog));
}
}


注意addImplicitCollection方法的调用,需要描述在某个类上的某个成员变量不需要被显示。

得到的结果基本上达到了要求: <blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</blog>

7,为属性添加别名
下一步是要把author成员变量设置为XML的属性。要做到这点,我们需要告诉XStream将author属性作为Blog类的"author"属性。
xstream.useAttributeFor(Blog.class,"author");
现在留给我们一个问题,XStream怎么讲一个Author转换成一个String对象让他在Blog节点中以author属性显示?
只需要使用SimpleValeConverter并且实现我们自己的Author转换器:
class AuthorConverter implements SingleValueConverter {
}
第一个需要实现的方法是告诉XStream该转化器是用来转换什么类型的对象:
public boolean canConvert(Class type) {
return type.equals(Author.class);
}
接下来是将一个Author实例转化成字符串:
public String toString(Object obj) {
return ((Author) obj).getName();
}
最后是相反的工作:怎么从一个字符串中得到Author实例
public Object fromString(String name) {
return new Author(name);
}
最后,该转化器看起来是这样:
class AuthorConverter implements SingleValueConverter {
public String toString(Object obj) {
return ((Author) obj).getName();
}
public Object fromString(String name) {
return new Author(name);
}
public boolean canConvert(Class type) {
return type.equals(Author.class);
}
}
然后将这个转化器注册到XStream:

public class Test {
public static void main(String[] args) {
Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
teamBlog.add(new Entry("tutorial",
"Today we have devel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值