Jibx插件的使用
【摘要】
Jibx是一款非常优秀的XML文件数据绑定的框架,提供灵活的绑定映射文件,实现数据对象和XML文件之间的转换,并不需要修改既有的Java,另外,它的转换效率是目前很多其他开源项目都无法比拟的。本文来演...
Jibx是一款非常优秀的XML文件数据绑定的框架,提供灵活的绑定映射文件,实现数据对象和XML文件之间的转换,并不需要修改既有的Java,另外,它的转换效率是目前很多其他开源项目都无法比拟的。本文来演示下如何使用
Jibx插件的使用
下载Jibx插件:
链接:https://pan.baidu.com/s/1Va9D8LZlxoVU5VndC7T1ag
提取码:oyjt
Pojo创建
Order类
package com.dpb.netty.xml.pojo;
/**
*
* @author 波波烤鸭
* @email dengpbs@163.com
*
*/
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 + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
生成绑定文件
我们想将绑定文件生成在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
- 1
说明
java -cp ..libx-tools.jar ..BindGen -t 生成文件保存地址 -v 需要绑定文件的class文件 完整包名.类名
- 1
对字节码增强
方式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
- 1
方式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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
执行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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
XML的序列化和反序列执行成功。
文章来源: dpb-bobokaoya-sm.blog.csdn.net,作者:波波烤鸭,版权归原作者所有,如需转载,请联系作者。
原文链接:dpb-bobokaoya-sm.blog.csdn.net/article/details/89290994
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)