java xml 绑定 —— castor框架

来源:互联网 发布:ubuntu虚拟机如何联网 编辑:程序博客网 时间:2024/05/13 14:55

java应用程序中经常要用到xml,那么如何将java对象和xml文档绑定,方便的互相转换是很多人关心的问题。现在有很多java xml绑定技术,比如xom(Elliotte Rusty Harold的开源类库,可以用www.xom.nu下载)非常好用,看起来最简单;还有java自身对xml的操作分成jaxp(java xml处理)和jaxb(java xml 绑定)两部分;还有我这次介绍的castor框架。

已经有很多很多非常好用的java xml绑定技术了,那么我就我自己用到的一点点皮毛介绍一些经验,免得大家和我一样走弯路。

1、首先下在castor需要的包,http://www.castor.org/ 建议直接下载 Full SVN snapshot: All sources, docs, and 3rd party libraries (big) ,其实这个包不是很大,标着big也就4.3M大小。

2、提取需要使用的包,与xml操作相关的包有
      castor-1.1.2.1.jar(在 Castor 主目录中)
      castor-1.1.2.1-xml.jar(在 Castor 主目录中)
      xerces-J-1.4.0.jar(放在与 Castor 依赖项库相同的位置) 
      commons-logging-1.1.jar(放在与 Castor 依赖项库相同的位置)

     将这几个包加到程序的classpath下。

3、将java对象与xml文档绑定

     我利用castor写了一个DataBind类

     import java.io.File;
     import java.io.FileInputStream;
     import java.io.FileNotFoundException;
     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.io.InputStreamReader;
     import java.io.OutputStreamWriter;
     import java.io.Reader;
     import java.io.UnsupportedEncodingException;
     import java.io.Writer;

     import org.exolab.castor.xml.MarshalException;
     import org.exolab.castor.xml.Marshaller;
     import org.exolab.castor.xml.Unmarshaller;
     import org.exolab.castor.xml.ValidationException;

     public class DataBind<T> {

           public void bind2XML(String path, T obj) {
                 try {
                             File file = new File(path);
                             Writer writer = new OutputStreamWriter(new FileOutputStream(file),
                                                                                                           "UTF-8");
                            Marshaller marshaller = new Marshaller(writer);
                            marshaller.setEncoding("UTF-8");
                           marshaller.marshal(obj);
                 } catch (MarshalException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                 } catch (ValidationException e) {
                            // TODO Auto-generated catch block
                          e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                           // TODO Auto-generated catch block
                          e.printStackTrace();
                } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                         e.printStackTrace();
                } catch (IOException e) {
                           // TODO Auto-generated catch block
                         e.printStackTrace();
                }
        }

        @SuppressWarnings("unchecked")
        public T bind2Class(String cName, String path) {
              T obj = null;

             try {
                       File file = new File(path);
                       Reader reader = new InputStreamReader(new FileInputStream(file),
                                                                                                        "UTF-8");
                       obj = (T) Unmarshaller.unmarshal(Class.forName(cName), reader);
                       reader.close();
                       } catch (MarshalException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                        } catch (ValidationException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                        } catch (UnsupportedEncodingException e) {
                                    // TODO Auto-generated catch block
                                   e.printStackTrace();
                        } catch (FileNotFoundException e) {
                                    // TODO Auto-generated catch block
                                  e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                        }  catch (IOException e) {
                           // TODO Auto-generated catch block
                         e.printStackTrace();
                       }

                        return obj;
             }
}

注意: Writer writer = new OutputStreamWriter(new FileOutputStream(file),
                                                                                                           "UTF-8");
           这个UTF-8不是写着玩的,由于我们用到中文(特别,为我们是中国人自豪),如果没有这个可能会产生中文乱码。

4、如何使用

        比如定义了一个类:

        public class Employee{
                    private String name;
                    private double salary;
                   
                    public Employee(){
                    }
                    
                    //下面Getter/Setter方法省略
        }

        需要将这个类的对象 Employee employee = new  Employee();

       绑定到一个xml文件可以这样:

       DataBind<Employee> databind = new DataBind<Employee>();

       databind.bind2XML("employee.xml", employee);

       就可以把employee对象写入到employee.xml中。

       反过来,employee = databind.bind2Class(Employee.class.getName(), "employee.xml");

       就可以把employee.xml中的内容变成一个Employee对象。

       **注意  想要这样绑定的类,必须要有一个默认构造方法(无参),对想要持久化的属性,必须要有Getter/Setter方法。

castor框架可以自己处理List或者Set接口的对象,非常方便。

 当然,castor框架还有很多强大的功能,有兴趣的朋友可以参考:

http://www.ibm.com/developerworks/cn/xml/x-xjavacastor1/     

我只是掌握一点点皮毛,抛砖引玉

 

 

原创粉丝点击