Jibx是一款非常优秀的XML文件数据绑定的框架,提供灵活的绑定映射文件,实现数据对象和XML文件之间的转换,并不需要修改既有的Java,另外,它的转换效率是目前很多其他开源项目都无法比拟的。本文来演示下如何使用
Jibx插件的使用
下载Jibx插件:
链接:https://pan.baidu.com/s/1Va9D8LZlxoVU5VndC7T1ag
提取码:oyjt
Pojo创建
Order类
package com.dpb.netty.xml.pojo;
/**
*
* @author 波波烤鸭
* @email [email protected]
*
*/
public class Order {
private long orderNumber;
private Customer customer;
/** Billing address information. */
private Address billTo;
private Shipping shipping;
/**
* Shipping address information. If missing, the billing address is also
* used as the shipping address.
*/
private Address shipTo;
private Float total;
public long getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(long orderId) {
this.orderNumber = orderId;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Address getBillTo() {
return billTo;
}
public void setBillTo(Address billTo) {
this.billTo = billTo;
}
public Shipping getShipping() {
return shipping;
}
public void setShipping(Shipping shipping) {
this.shipping = shipping;
}
public Address getShipTo() {
return shipTo;
}
public void setShipTo(Address shipTo) {
this.shipTo = shipTo;
}
public Float getTotal() {
return total;
}
public void setTotal(Float total) {
this.total = total;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Order [orderNumber=" + orderNumber + ", customer=" + customer
+ ", billTo=" + billTo + ", shipping=" + shipping.toString()
+ ", shipTo=" + shipTo + ", total=" + total + "]";
}
}
生成绑定文件
我们想将绑定文件生成在src/main/resources/jibx目录下,进入cmd下,切换到maven项目的target/classes目录下
然后执行如下命令
java -cp c:\tools\工具\jibx_1_3_1\jibx\lib\jibx-tools.jar org.jibx.binding.generator.BindGen -t C:\tools\workspace\workspace-csdn\NettyLearn\src\main\resources\jibx -v com.dpb.netty.xml.pojo.Customer com.dpb.netty.xml.pojo.Shipping com.dpb.netty.xml.pojo.Address com.dpb.netty.xml.pojo.Order com.dpb.netty.xml.pojo.OrderFactory
说明
java -cp ..libx-tools.jar ..BindGen -t 生成文件保存地址 -v 需要绑定文件的class文件 完整包名.类名
对字节码增强
方式1:jibx-bind.jar增强
未增强前:
执行如下命令:
java -cp c:\tools\工具\jibx_1_3_1\jibx\lib\jibx-bind.jar org.jibx.binding.Compile -v C:\tools\workspace\workspace-csdn\NettyLearn\src\main\resources\jibx\binding.xml
方式2:maven插件动态增强
在maven项目中如果能够通过插件动态的增强,那么实现起来就比较方便,实现步骤如下:
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-run</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-extras</artifactId>
<version>1.3.1</version>
</dependency>
<build>
<plugins>
<plugin>
<!-- 生成jibx class信息 -->
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<schemaBindingDirectory>${basedir}/src/main/resources/jibx</schemaBindingDirectory>
<includeSchemaBindings>
<includeSchemaBindings>*binding.xml</includeSchemaBindings>
</includeSchemaBindings>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<id>jibx-bind</id>
<phase>compile</phase>
<!-- 把jibx绑定到了comile编译阶段 -->
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行install即可增强class文件
测试
执行如下测试代码
package com.dpb.netty.xml;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import com.dpb.netty.xml.pojo.Order;
import com.dpb.netty.xml.pojo.OrderFactory;
public class TestOrder {
private IBindingFactory factory = null;
private StringWriter writer = null;
private StringReader reader = null;
private final static String CHARSET_NAME = "UTF-8";
private String encode2Xml(Order order) throws JiBXException, IOException {
factory = BindingDirectory.getFactory(Order.class);
writer = new StringWriter();
IMarshallingContext mctx = factory.createMarshallingContext();
mctx.setIndent(2);
mctx.marshalDocument(order, CHARSET_NAME, null, writer);
String xmlStr = writer.toString();
writer.close();
System.out.println(xmlStr.toString());
return xmlStr;
}
private Order decode2Order(String xmlBody) throws JiBXException {
reader = new StringReader(xmlBody);
IUnmarshallingContext uctx = factory.createUnmarshallingContext();
Order order = (Order) uctx.unmarshalDocument(reader);
return order;
}
public static void main(String[] args) throws JiBXException, IOException {
TestOrder test = new TestOrder();
Order order = OrderFactory.create(123);
String body = test.encode2Xml(order);
Order order2 = test.decode2Order(body);
System.out.println(order2);
}
}
XML的序列化和反序列执行成功。