不需要生成dtd,无用配置,不需要生成辅助类,速度快。这就是xstream+xpp超强黄金组合。
xstream大家都知道啦,xml pull parser是一种高速的 解析xml文件的方式,速度要比传统方式快很多(发现pull式解析现在比较流行了)。下面我给出多种使用方法的例子。
1.最简单的使用方法
1. public static void write() {
2. xstream sm = new xstream();
3. mytest t = new mytest();
4. t.setname("moogle");
5. t.setxb("男");
6. try {
7. fileoutputstream ops = new fileoutputstream(new file("c://111.xml"));
8. sm.toxml(t, ops);
9. ops.close();
10. } catch (exception e) {
11. e.printstacktrace();
12. }
13. }
14. public static void read() {
15. xstream sm = new xstream(new domdriver());
16. try {
17. fileinputstream ops = new fileinputstream(new file("c://111.xml"));
18. mytest t = (mytest)sm.fromxml(ops);
19. system.out.println(t.getname());
20. ops.close();
21. } catch (exception e) {
22. e.printstacktrace();
23. }
24. }
生成 xml是
# <mytest>
# <name>asd</name>
# <xb>男</xb>
2.中等方法(需要1.2版以上才有这个功能)
xstream stream = new xstream();
stream.alias("schema", schemaobject.class);
stream.useattributefor("url", string.class);
stream.useattributefor("jdbcdriverclass", string.class);
stream.useattributefor("user", string.class);
stream.useattributefor("password", string.class);
fileoutputstream s = new fileoutputstream(file);
stream.toxml(theobject, s);
s.close();
alias和useattributefor是用作把 <com.hongsoft.model.schemaobject>修改为<schema>
3.高级方法
stream.registerconverter(new schemaxmlconvertor());
fileinputstream s = new fileinputstream(file);
object = (schemaobject) stream.fromxml(s);
registerconverter可以实现把任何schema的xml和object互相转换,这个其实已经否定了很多朋友说的
“xstream+xpp”功能不强的说法。schemaxmlconvertor示例如下:
public void marshal(object arg0, hierarchicalstreamwriter writer,
marshallingcontext arg2) {
schemaobject schema=(schemaobject)arg0;
writer.startnode(schemaobject.table);
writer.addattribute(schemaobject.table_name, itable.getname());
//line
list<schemaobject.tableobject.lineobject> lines=itable.getlines();
for(int j=0;j<lines.size();j++)
{
schemaobject.tableobject.lineobject jline=lines.get(j);
writer.startnode(schemaobject.line);
//column
map<string,string> columns=jline.getcolumns();
iterator ite=columns.keyset().iterator();
while(ite.hasnext())
{
string columnname=ite.next().tostring();
writer.addattribute(columnname, columns.get(columnname));
}
writer.endnode();
writer.underlyingwriter();
}
public object unmarshal(hierarchicalstreamreader reader,
unmarshallingcontext arg1) {
schemaobject schema=new schemaobject();
schema.setjdbcdriverclass(reader.getattribute(schemaobject.jdbc_driver_class));
schema.seturl(reader.getattribute(schemaobject.url));
schema.setuser(reader.getattribute(schemaobject.user));
schema.setpassword(reader.getattribute(schemaobject.password));
list<tableobject> tables = new arraylist<tableobject>();
while(reader.hasmorechildren()) {
reader.movedown();
schemaobject.tableobject table=new schemaobject().new tableobject();
table.setname(reader.getattribute(schemaobject.table_name));
tables.add(table);
reader.moveup();
schema.settables(tables);
return schema;
}
public boolean canconvert(class arg0) {
return arg0.equals(schemaobject.class);
说明:
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");
意思是先为java成员定义个别名,然后在将别名应用于xml属性上。
<investor gdxm="hahhah">
这些问题虽然解决了,但又发现了新的问题:xml转java时,当java中没有定义xml元素节点时,这时候会抛出异常。也许通过xstream本身的api可以解决,也许是xstream本身所不能处理的问题,时间有限,也没来得及深究。有知道的朋友,还望留言。
4、再进行xml到java 的转换过程中,xstream对象别名可以定义很多,涵盖的类的范围超过xml所能表达的范围,这个也没有关系,xstream还是会忠实地将xml还原为对象。但是如果xml范围大于xstream所涵盖类的范围,那么转换过程会出错。比如,要将一个investor节点转换为manibody对象,肯定会出错。