Jibx简单示例
@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
首先从 JiBX 网站下载 JiBX,当前最新版本是 beta 3。解开下载的 zip 文件,里面有一个 lib 目录,包含了 bcel.jar, jibx-bind.jar, jibx-extras.jar, jibx-run.jar, xpp3.jar 五个 jar 文件。bcel.jar, jibx-bind.jar 只有在 binding compiler 的时候才用得到。jibx-extras.jar 是一个可选的工具包,里面有一些测试和验证的工具类。
1.定义一个我们将要处理 XML 文件,文件名为 data.xml,内容如下:
2.接着定义两个 Java 类 Customer 和 Person,也采用最简单的方式,用对象的域值对应元素,内容如下:
public class Customer {
public Person person;
public String street;
public String city;
public String state;
public Integer zip;
public String phone;
}
public class Person {
public int customerNumber;
public String firstName;
public String lastName;
} 这个两个类没有任何方法,够简单吧!或许你已经看出来了,Customer 类的七个 field 对应的是 XML 文件中 customer 元素的七个子元素。Person 类的三个 field 对应的是 person 元素的三个子元素。在 Person 类的 field 的名称并不是和 person 元素的子元素名称完全相等,这是遵守 Java 编程规范 field 命名的需要,虽然不相等,但这不重要,可以在绑定定义文挡中把它们一一对应起来。
3.绑定定义文挡
绑定定义文挡是依据绑定定义规范将 XML 数据和 Java 对象绑定的 XML 文挡。文件名为 binding.xml,内容如下:
binding.xml 文件中的 name 和 field 属性分别将 XML 中的元素和 Java 对象中的 field 一一对应并绑定起来。
<mapping name="customer" class="Customer">
mapping 元素的 name 和 class 属性将 customer 根元素和 Customer 类绑定在一起。
<structure name="person" field="person">
public Person person;
上面两行定义了 person 是 Customer 的 field,同时也把 person 元素和 person 类绑定在一起。
以下命令是在 Linux 下执行,如果是 Windows 平台请转换成相应的命令
5.执行 binding runtime 过程
接着写一个简单的读取 data.xml 测试程序 Test.java,内容如下:
1.定义一个我们将要处理 XML 文件,文件名为 data.xml,内容如下:
<
customer
>
< person >
< cust-num > 123456789 </ cust-num >
< first-name > John </ first-name >
< last-name > Smith </ last-name >
</ person >
< street > 12345 Happy Lane </ street >
< city > Plunk </ city >
< state > WA </ state >
< zip > 98059 </ zip >
< phone > 888.555.1234 </ phone >
</ customer >
这个 XML 文件非常简单,共有十个元素,没有属性。根元素 customer 有 person, street, city, state, zip, phone 六个子元素。其中元素 person 有 cust-num, first-name, last-name 三个子元素。 < person >
< cust-num > 123456789 </ cust-num >
< first-name > John </ first-name >
< last-name > Smith </ last-name >
</ person >
< street > 12345 Happy Lane </ street >
< city > Plunk </ city >
< state > WA </ state >
< zip > 98059 </ zip >
< phone > 888.555.1234 </ phone >
</ customer >
2.接着定义两个 Java 类 Customer 和 Person,也采用最简单的方式,用对象的域值对应元素,内容如下:
public class Customer {
public Person person;
public String street;
public String city;
public String state;
public Integer zip;
public String phone;
}
public class Person {
public int customerNumber;
public String firstName;
public String lastName;
}
3.绑定定义文挡
绑定定义文挡是依据绑定定义规范将 XML 数据和 Java 对象绑定的 XML 文挡。文件名为 binding.xml,内容如下:
binding.xml 文件中的 name 和 field 属性分别将 XML 中的元素和 Java 对象中的 field 一一对应并绑定起来。
<mapping name="customer" class="Customer">
mapping 元素的 name 和 class 属性将 customer 根元素和 Customer 类绑定在一起。
<structure name="person" field="person">
public Person person;
上面两行定义了 person 是 Customer 的 field,同时也把 person 元素和 person 类绑定在一起。
<
binding
>
< mapping name ="customer" class ="Customer" >
< structure name ="person" field ="person" >
< value name ="cust-num" field ="customerNumber" />
< value name ="first-name" field ="firstName" />
< value name ="last-name" field ="lastName" />
</ structure >
< value name ="street" field ="street" />
< value name ="city" field ="city" />
< value name ="state" field ="state" />
< value name ="zip" field ="zip" />
< value name ="phone" field ="phone" />
</ mapping >
</ binding >
4.执行 Binding Compiler 过程 < mapping name ="customer" class ="Customer" >
< structure name ="person" field ="person" >
< value name ="cust-num" field ="customerNumber" />
< value name ="first-name" field ="firstName" />
< value name ="last-name" field ="lastName" />
</ structure >
< value name ="street" field ="street" />
< value name ="city" field ="city" />
< value name ="state" field ="state" />
< value name ="zip" field ="zip" />
< value name ="phone" field ="phone" />
</ mapping >
</ binding >
以下命令是在 Linux 下执行,如果是 Windows 平台请转换成相应的命令
#javac Person.java
#javac - classpath . Customer.java
#java - jar lib / jibx - bind.jar binding.xml
执行完后,在当前目录多了四个 class 文件,分别是 Person.class, Customer.class, JiBX_bindingCustomer_access.class, JiBX_bindingFactory.class。 #javac - classpath . Customer.java
#java - jar lib / jibx - bind.jar binding.xml
5.执行 binding runtime 过程
接着写一个简单的读取 data.xml 测试程序 Test.java,内容如下:
import
java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.jibx.runtime.JiBXException;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IUnmarshallingContext;
class Test {
public static void main(String[] args) {
try {
IBindingFactory bfact = BindingDirectory.getFactory(Customer. class );
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Customer customer = (Customer)uctx.unmarshalDocument( new FileInputStream( " data.xml " ), null );
Person person = customer.person;
System.out.println( " cust-num: " + person.customerNumber);
System.out.println( " first-name: " + person.firstName);
System.out.println( " last-name: " + person.lastName);
System.out.println( " street: " + customer.street);
} catch (FileNotFoundException e){
System.out.println(e.toString());
} catch (JiBXException e){
System.out.println(e.toString());
}
}
}
编译并运行这个测试程序 import java.io.FileNotFoundException;
import org.jibx.runtime.JiBXException;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IUnmarshallingContext;
class Test {
public static void main(String[] args) {
try {
IBindingFactory bfact = BindingDirectory.getFactory(Customer. class );
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Customer customer = (Customer)uctx.unmarshalDocument( new FileInputStream( " data.xml " ), null );
Person person = customer.person;
System.out.println( " cust-num: " + person.customerNumber);
System.out.println( " first-name: " + person.firstName);
System.out.println( " last-name: " + person.lastName);
System.out.println( " street: " + customer.street);
} catch (FileNotFoundException e){
System.out.println(e.toString());
} catch (JiBXException e){
System.out.println(e.toString());
}
}
}
#javac
-
classpath .:lib
/
jibx
-
run.jar Test.java
#java - cp .:lib / jibx - run.jar:lib / xpp3.jar Test
程序运行的结果是 #java - cp .:lib / jibx - run.jar:lib / xpp3.jar Test
cust
-
num:
123456789
first - name:John
last - name:Smith
street: 12345 Happy Lane
first - name:John
last - name:Smith
street: 12345 Happy Lane
Kyle Wang