xstream+xpp超强黄金组合使用方法

本文介绍了XStream结合XPP的高效XML处理方法,通过三种使用案例展示了如何简化XML生成及解析过程,包括基本用法、中等难度的属性映射及高级自定义转换器的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不需要生成dtd,无用配置,不需要生成辅助类,速度快。这就是xstream+xpp超强黄金组合。
xstream大家都知道啦,XML Pull Parser是一种高速的 解析xml文件的方式,速度要比传统方式快很多(发现pull式解析现在比较流行了)。下面我给出多种使用方法的例子。

1.最简单的使用方法
因为这个太简单,所以我从moogle的blog http://moogle.javaeye.com/blog/34661取下面的例子
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.高级方法
XStream stream = new XStream();
stream.registerConverter(new SchemaXMLConvertor());
stream.alias("schema", SchemaObject.class);
FileInputStream s = new FileInputStream(file);
object = (SchemaObject) stream.fromXML(s);
s.close();
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.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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值