castor xml解析

来源:互联网 发布:兄弟连php培训费用 编辑:程序博客网 时间:2024/05/29 19:41


使用xstream的bean转xml,总有问题,类的名字一长串,还包含$$enhanced_cglib 字样,因为用的是hibernate,取出来的bean都是cglib的代理类,转成xml后,里面都包含$$enhanced_cglib  这样的类,怎么都去不掉。
改使用另一个beantoxml的工具 castorMarshaller,他定制xml的方式还行。但发现转成xml是同样的问题,无法正确识别cglib,而且更夸张。

解决办法:看看castor有没有解决办法,官网上一搜,老外说1.2版本解决代理类的问题是好的,到了1.3.1反而不行了,
castor对于自定制xml,增加个mapping文件


BeanToXml.java
        File file = new File("file/Person1.xml");
            Writer writer = new FileWriter(file);
            Marshaller mar = new Marshaller(writer);
            Mapping map = new Mapping();
            map.loadMapping("file/mapping_beanToxml.xml");
            mar.setMapping(map);
        mar.marshal(beans);

XmlToBean.java
        FileReader reader = new FileReader("book.xml");
        Book book = (Book) Unmarshaller.unmarshal(Book.class, reader);


        Mapping mapping = new Mapping();
        mapping.loadMapping("book-mapping.xml");
        FileReader reader = new FileReader("book.xml");
        Unmarshaller unmarshaller = new Unmarshaller(Book.class);
        unmarshaller.setMapping(mapping);

原创粉丝点击