ITeye博客

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

hongsoft · 2007-04-18 10:28 · Markdown

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

1.最简单的使用方法
因为这个太简单,所以我从moogle的blog http://moogle.iteye.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);
}

评论(1)

  1. #1 楼 · xgrass18 · 2010-04-22 13:41

    对象中包含list或者引用其他对象呢?

评论功能维护中,当前仅展示历史评论。