jibx简介

来源:百度文库 编辑:神马文学网 时间:2024/05/07 11:06:38
(1)jibx简介jibx又一个不错的xml绑定工具,随着这段时间的使用,感觉越来越随心应手了。和jaxb一样,都是属于xml绑定工具。不同于jaxb,jibx使用java字节码enhance技术,而jaxb更多在于源代码生成技术。jibx的工作主要在于前期,也就是进行字节码绑定,这一部分基本上都是在编译器完成的。在运行期,不需要任何的配置,由于字节码已经嵌入java类中。而jaxb更多在于运行期绑定,通过元数据或者xsd文件进行解析绑定。相对于 jaxb来说,jibx更加的快速以及灵活。不过,前期的编译工作还是需要花费一点时间熟悉。JiBX 是一个绑定 XML 数据到 Java 对象的框架。JiBX 用一个绑定定义文挡(binding definition document)来定义 XML 数据与 Java 对象转换的规则,这个文挡就是联系 XML 数据与 Java 对象之间的桥梁。
这里有必要先介绍两个数据绑定术语 marshal 和 unmarshal,marshal 是由 Java 对象生成 XML 文挡,unmarshal 是根据 XML 文挡建立 Java 对象。
使用 JiBX 的过程分成两个过程,一个是 binding compiler,另一个是 binding runtime。binding compiler 是一个前期准备过程,包括定义绑定定义文挡,定义与 XML 绑定在一起的 Java 对象,然后编译。在这个过程,JiBX 比其他项目在操作上要简单,不用定义 DTD 和 Schema,缺点是需要自己定义 Java 程序。binding runtime 是使用 binding compiler 编译好的 Java class 处理 XML 数据。
JiBX 也用到了第三方的工具 XPP3 Pull Parser 和 BCEL,在 JiBX 发布的文件中也包含了这两个工具相关的文件。需要用到的JAR文件有:bcel.jar, jibx-bind.jar, jibx-extras.jar, jibx-run.jar, xpp3.jar 五个 jar 文件(2)实例:一,先定义各JAVA类:package com.hch.testjibx;

public class Address {
    
  private String addressName;
        private String city;
        private String nation;
        
  public String getAddressName() {
    return addressName;
   }
  public String getCity() {
    return city;
   }
  public String getNation() {
    return nation;
   }
}


package com.hch.testjibx;

public class Company {
    
  private String comName;
  private String address;
    
  public String getAddress() {
    return address;
   }
  public String getComName() {
    return comName;
   }
}

package com.hch.testjibx;

import java.util.ArrayList;

public class Customer {
    
  private String firstName;
  private String lastName;
  private int age;
  private String phone;        
  private Address address;
  private ArrayList companyList;
        
  public Address getAddress() {
    return address;
   }
  public int getAge() {
    return age;
   }
  public ArrayList getCompanyList() {
    return companyList;
   }
  public String getFirstName() {
    return firstName;
   }
  public String getLastName() {
    return lastName;
   }
  public String getPhone() {
    return phone;
   }
}二,定义XML文件 -- data.xml    
    
     John
     Smith
     32
     888.555.1234
    

         Plunk
       WA
       china zhejiang province hangzhou city xihu district wenerxi road No 48
    

    
       pubone
      
zhejiang university

    

    
       jaoee
      
hongcheng techenologe

    

三定义binding.xml
     "customer" class="com.hch.testjibx.Customer">
         "first_name" field="firstName"/>
         "last_name" field="lastName"/>
         "age" field="age"/>
         "phone" field="phone"/>
         "address" field="address">
             "city" field="city"/>
             "nation" field="nation"/>
             "address_name" field="addressName"/>
        

         "companyList">
           "company" type="com.hch.testjibx.Company">
             "com_name" field="comName"/>
             "address" field="address"/>
        

        

    

四,定义ant的build.xml,用ant来进行编译,而不是采用手动编译(比较麻烦)"1.0" encoding="gb2312"?>
"testjibx" default="run">
  
   "prjBase" value="."/>
   "src" value="${prjBase}/src"/>
   "lib" value="${prjBase}/lib"/>
   "classes" value="${prjBase}/classes"/>
    
   "clean">
     "true">
       "${classes}" includes="**/*" defaultexcludes="no"/>
    
    
  

    
  
    
  
    
    
  
    
      
      
        
        
        
        
        
      

    

    
       />
    

  

    
   "bind"
classname="org.jibx.binding.ant.CompileTask" classpath="${lib}/jibx-bind.jar"/>
    
   "jibx-binding"
depends="compile">
     "false" load="true" binding="${classes}/binding.xml">
      
         "${classes}">
         "${lib}/jibx-bind.jar"/>
      

    

  
    
   "run" depends="jibx-binding" description="run">
     "com.hch.testjibx.Test">
      
         "${classes}">
         "${lib}/bcel.jar"/>
         "${lib}/jibx-bind.jar"/>
         "${lib}/jibx-extras.jar"/>
         "${lib}/jibx-run.jar"/>
         "${lib}/xpp3.jar"/>
      

    

  

    
五:定义测试类:package com.hch.testjibx;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;

public class Test {

  /**
     * @param args
     */

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
       IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
        // unmarshal customer information from file
         IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
         File dataFile = new File("./src/data.xml");
         System.out.println("filepath:"+dataFile.getAbsolutePath());
         InputStream in = new FileInputStream(dataFile);
         Customer customer = (Customer)uctx.unmarshalDocument(in, null);
         System.out.println("customer.firstName:"+customer.getFirstName());
         System.out.println("customer.lastName:"+customer.getLastName());
         System.out.println("customer.age:"+customer.getAge());
         System.out.println("customer.phone:"+customer.getPhone());
         System.out.println("customer.address.addressName:"+customer.getAddress().getAddressName());
         System.out.println("customer.address.city:"+customer.getAddress().getCity());
         System.out.println("customer.address.nation:"+customer.getAddress().getNation());
         System.out.println("((Company)customer.companyList.get(0)).comName:"+((Company)customer.getCompanyList().get(0)).getComName());
         System.out.println("((Company)customer.companyList.get(0)).address:"+((Company)customer.getCompanyList().get(0)).getAddress());
         System.out.println("((Company)customer.companyList.get(1)).comName:"+((Company)customer.getCompanyList().get(1)).getComName());
         System.out.println("((Company)customer.companyList.get(1)).address:"+((Company)customer.getCompanyList().get(1)).getAddress());
     }catch(Exception e){
       e.printStackTrace();
     }
   }

}六:运用ant后的结果:Buildfile: D:\Eclipse3.2\_Eclipse\zongheTest\testjibx\build.xml
init:
clean:
   [delete] Deleting 9 files from D:\Eclipse3.2\_Eclipse\zongheTest\testjibx\classes
   [delete] Deleted 3 directories from D:\Eclipse3.2\_Eclipse\zongheTest\testjibx\classes
compile:
    [javac] Compiling 4 source files to D:\Eclipse3.2\_Eclipse\zongheTest\testjibx\classes
     [copy] Copying 4 files to D:\Eclipse3.2\_Eclipse\zongheTest\testjibx\classes
jibx-binding:
run:
     [java] filepath:D:\Eclipse3.2\_Eclipse\zongheTest\testjibx\.\src\data.xml
     [java] customer.firstName:John
     [java] customer.lastName:Smith
     [java] customer.age:32
     [java] customer.phone:888.555.1234
     [java] customer.address.addressName:china zhejiang province hangzhou city xihu district wenerxi road No 48
     [java] customer.address.city:Plunk
     [java] customer.address.nation:WA
     [java] ((Company)customer.companyList.get(0)).comName:pubone
     [java] ((Company)customer.companyList.get(0)).address:zhejiang university
     [java] ((Company)customer.companyList.get(1)).comName:jaoee
     [java] ((Company)customer.companyList.get(1)).address:hongcheng techenologe
BUILD SUCCESSFUL
Total time: 3 seconds