1、给一个例子:
<binding xmlns:ns1="http://vteba.com/service/xml/jibx" name="bind" package="com.vteba.service.xml.jibx"> <namespace uri="http://vteba.com/service/xml/jibx" default="elements"/> <mapping abstract="true" type-name="ns1:customer" class="com.vteba.service.xml.jibx.Customer"> <structure map-as="ns1:person" field="person" usage="optional" name="person"/> <value style="element" name="street" field="street" usage="optional"/> <value style="element" name="city" field="city" usage="optional"/> <value style="element" name="state" field="state" usage="optional"/> <value style="attribute" name="zip" field="zip" usage="optional"/> <value style="element" name="phone" field="phone" usage="optional"/> <collection field="nameList" usage="optional" create-type="java.util.ArrayList"> <value name="name" type="java.lang.String"/> </collection> <collection field="personList" usage="optional" create-type="java.util.ArrayList"> <structure map-as="ns1:person" name="person"/> </collection> </mapping> <mapping class="com.vteba.service.xml.jibx.Customer" name="customer"> <structure map-as="ns1:customer"/> </mapping> <mapping abstract="true" type-name="ns1:person" class="com.vteba.service.xml.jibx.Person"> <value style="attribute" name="customerNumber" field="customerNumber"/> <value style="element" name="firstName" field="firstName" usage="optional"/> <value style="element" name="lastName" field="lastName" usage="optional"/> </mapping> </binding>
2、这个文件是使用jibx提供的工具生成。不用在手工去写了。当然你可以参考jibx提供的20多个例子来写。
jibx工具类,org.jibx.binding.generator.BindGen或org.jibx.binding.BindingGenerator这两个类都可以,前者在jibx-tools.jar
中,后者在jibx-bind.jar中。
3、命令如下:
4、当然你可以把这些命令写在ant脚本中,或者使用java main方法来执行。此处直接使用命令行。
我使用的是maven,在命令行中切换到target文件夹下的classes目录。
上面的java 是运行某个程序 –cp是依赖的classpath路径的jar、zip等文件,-b 是输出文件名称,是BindGen类的参数。这样会在classes目录中生成bind.xml文件和jibx.xsd文件。bind.xml既是开头贴出来的文件。
5、然后你可以通过上述bind.xml文件,对jibx类进行字节码增强。
有两种方式可以使用,一是使用ant,官方提供了很多例子,可以自己参考写。
二是是jibx eclipse maven插件。会自动绑定,推荐使用这个。可以到网上去下载
上面还有一个jibx.xsd文件,可以根据他产生java类。
6、下面既是使用类
package com.vteba.service.xml.jibx; import java.util.List; public class Customer { public Person person; public String street; public String city; public String state; public Integer zip; public String phone; public List<String> nameList; public List<Person> personList; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Integer getZip() { return zip; } public void setZip(Integer zip) { this.zip = zip; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public List<Person> getPersonList() { return personList; } public void setPersonList(List<Person> personList) { this.personList = personList; } public List<String> getNameList() { return nameList; } public void setNameList(List<String> nameList) { this.nameList = nameList; } }
public class Person { public int customerNumber; public String firstName; public String lastName; public int getCustomerNumber() { return customerNumber; } public void setCustomerNumber(int customerNumber) { this.customerNumber = customerNumber; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
7、这个例子已经包含了,一般类的映射,List<JavaBean> List<String>。
如果将List<String> 换成String[],create-type属性就不需要指定了。
关于绑定文件的各个属性的意思,请参考官方文档。attribute是作为属性,element是作为子元素。
对于collection,如果不指定name属性,映射的xml将不会有最外层元素。
8、和xstream做了性能测试,结果如下:
9、性能基本上是xstream的2倍多,接近三倍。我做过jaxb和xstream的性能测试,xstream基本上是jaxb的2倍多。
jibx目前是性能最好的xml映射和序列化框架了。
10、另外,jibx对中文不够友好,你如果使用utf-8,是没有问题的,gbk的话,需要自己实现Escaper。待续。
jibx对map的序列化也不够友好,不过jibx也提供了支持,不过要稍作扩展,待续。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|