jibx用法

(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
<customer>    
    
    <first_name>John</first_name>
    <last_name>Smith</last_name>
    <age>32</age>
    <phone>888.555.1234</phone>
    <address>
        <city>Plunk</city>
      <nation>WA</nation>
      <address_name>china zhejiang province hangzhou city xihu district wenerxi road No 48</address_name>
    </address>
    <company>
      <com_name>pubone</com_name>
      <address>zhejiang university</address>
    </company>
    <company>
      <com_name>jaoee</com_name>
      <address>hongcheng techenologe</address>
    </company>
</customer>
三定义binding.xml
<binding>
    <mapping name= "customer" class= "com.hch.testjibx.Customer">
        <value name= "first_name" field= "firstName"/>
        <value name= "last_name" field= "lastName"/>
        <value name= "age" field= "age"/>
        <value name= "phone" field= "phone"/>
        <structure name= "address" field= "address">
            <value name= "city" field= "city"/>
            <value name= "nation" field= "nation"/>
            <value name= "address_name" field= "addressName"/>
        </structure>
        <collection field= "companyList">
          <structure name= "company" type= "com.hch.testjibx.Company">
             <value name= "com_name" field= "comName"/>
             <value name= "address" field= "address"/>
         </structure>
        </collection>
    </mapping>
</binding>
 
四,定义ant的build.xml,用ant来进行编译,而不是采用手动编译(比较麻烦)
<?xml version= "1.0" encoding= "gb2312"?>
<project name= "testjibx" default= "run">
  <!--    
        可以用绝对路径来表示:value= "D:/Eclipse3.2/_Eclipse/zongheTest/testjibx"
         也可用相对路径来表示:value= "."    此方式比较好,适合代码移位。
  -->
  <property name= "prjBase" value= "."/>
  <property name= "src" value= "${prjBase}/src"/>
  <property name= "lib" value= "${prjBase}/lib"/>
  <property name= "classes" value= "${prjBase}/classes"/>
    
  <target name= "clean">
    <delete includeEmptyDirs= "true">
      <fileset dir= "${classes}" includes= "**/*" defaultexcludes="no"/>
    </delete>    
  </target>
    
  <target name="init">
    <mkdir dir="${classes}"/>
  </target>    
    
  <target name="compile" depends="init,clean"    description="编译源代码">
    <javac destdir="${classes}" verbose="false" debug="on" deprecation="true">
      <src path="${src}" />
      <classpath>
        <pathelement location="${lib}/bcel.jar"/>
        <pathelement location="${lib}/jibx-bind.jar"/>
        <pathelement location="${lib}/jibx-extras.jar"/>
        <pathelement location="${lib}/jibx-run.jar"/>
        <pathelement location="${lib}/xpp3.jar"/>
      </classpath>
    </javac>
    <copy todir="${classes}">
      <fileset dir="${src}" excludes="**/*.java" />
    </copy>
  </target>
    
  <taskdef name= "bind" classname= "org.jibx.binding.ant.CompileTask" classpath= "${lib}/jibx-bind.jar"/>
    
  <target name= "jibx-binding" depends= "compile">
    <bind verbose= "false" load= "true" binding= "${classes}/binding.xml">
      <classpath>
        <path location= "${classes}"></path>
        <pathelement location= "${lib}/jibx-bind.jar"/>
      </classpath>
    </bind>
  </target>
    
  <target name= "run" depends= "jibx-binding" description= "run">
    <java classname= "com.hch.testjibx.Test">
      <classpath>
        <path location= "${classes}"></path>
        <pathelement location= "${lib}/bcel.jar"/>
        <pathelement location= "${lib}/jibx-bind.jar"/>
        <pathelement location= "${lib}/jibx-extras.jar"/>
        <pathelement location= "${lib}/jibx-run.jar"/>
        <pathelement location= "${lib}/xpp3.jar"/>
      </classpath>
    </java>
  </target>
    
</project>
 
五:定义测试类:
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
 

你可能感兴趣的:(职场,休闲,jibx)