高效XML绑定框架JIBX
高效XML绑定框架JIBX
demo源码地址
https://gitee.com/clover-clover/clover.git
具体路径:
clover/clover-frame/clover-frame-netty/src/main/java/com/clover/frame/netty/http/xml/pojo
开发步骤
1-maven添加JIBX相关依赖
<!-- JiBx -->
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-run</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-extras</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-bind</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-tools</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-schema</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.bcel</groupId>
<artifactId>bcel</artifactId>
<version>6.7.0</version>
</dependency>
2-编写POJO类
Shipping
public enum Shipping {
STANDARD_MAIL, PRIORITY_MAIL, INTERNATIONAL_MAIL, DOMESTIC_EXPRESS, INTERNATIONAL_EXPRESS
}
Address
public class Address {
private String name;
private String city;
// setter and getter
}
Customer
public class Customer {
private long number;
private String firstName;
private String lastName;
private List<String> middleNames;
// setter and getter
}
Order
public class Order {
private long number;
private Customer customer;
private Address billTo;
private Shipping shipping;
private Address shipTo;
private Float total;
// setter and getter
}
3-使用ant来生成xml和POJO对象的绑定关系
IDEA如果没有ant窗口通过此方式添加:
在ant窗口点击【+】将build.xml添加到ant中,点击执行main任务即可。
如果在执行测试任务是,报错:如果报缺少 JiBX_bindingList,点击图中的bind即可解决。
binding.xml内容
<binding xmlns:ns1="http://clover.com/frame/netty/http/xml/pojo" name="binding" package="com.clover.frame.netty.http.xml.pojo">
<namespace uri="http://clover.com/frame/netty/http/xml/pojo" default="elements"/>
<mapping abstract="true" type-name="ns1:order" class="com.clover.frame.netty.http.xml.pojo.Order">
<value style="attribute" name="number" field="number"/>
<structure field="customer" usage="optional" name="customer">
<value style="attribute" name="number" field="number"/>
<value style="element" name="firstName" field="firstName" usage="optional"/>
<value style="element" name="lastName" field="lastName" usage="optional"/>
<collection field="middleNames" usage="optional" create-type="java.util.ArrayList">
<value name="middleName" type="java.lang.String"/>
</collection>
</structure>
<structure map-as="ns1:address" field="billTo" usage="optional" name="billTo"/>
<value style="element" name="shipping" field="shipping" usage="optional"/>
<structure map-as="ns1:address" field="shipTo" usage="optional" name="shipTo"/>
<value style="attribute" name="total" field="total" usage="optional"/>
</mapping>
<mapping class="com.clover.frame.netty.http.xml.pojo.Order" name="order">
<structure map-as="ns1:order"/>
</mapping>
<mapping abstract="true" type-name="ns1:address" class="com.clover.frame.netty.http.xml.pojo.Address">
<value style="element" name="name" field="name" usage="optional"/>
<value style="element" name="city" field="city" usage="optional"/>
</mapping>
</binding>
target中生成的class
JiBX_******.class类文件内容都空的
ant build.xml具体配置
build.xml配置文件中,bindgen任务需要特别注意,不要使用org.jibx.binding.BindingGenerator这个类,使用此类在后续的测试中发现有两个BUG:
集合属性报错-生成xml时
java.lang.String cannot be cast to org.jibx.runtime.IMarshallable
枚举属性报错-生成POJO对象时
该枚举没有可用的构造方法
<?xml version="1.0" encoding="utf-8"?>
<project default="main" basedir=".">
<path id="classpath">
<dirset dir="${basedir}/target/classes"/>
<!--下面目录为本地maven仓库的jibx和bcel的jar包的绝对路径-->
<fileset dir="/Users/zhangdq/.m2/repository/org/jibx/jibx-bind/1.4.2/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/org/jibx/jibx-run/1.4.2/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/org/jibx/jibx-tools/1.4.2/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/org/jibx/jibx-schema/1.4.2/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/org/apache/bcel/bcel/6.7.0/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/log4j/log4j/1.2.17/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/com/thoughtworks/qdox/qdox/1.12.1/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/org/apache/commons/commons-lang3/3.12.0/" includes="*.jar"/>
<fileset dir="/Users/zhangdq/.m2/repository/xpp3/xpp3/1.1.3.4.O/" includes="*.jar"/>
</path>
<!--这个是主任务 , depends 依赖下面写的三个分任务 -->
<target name="main" depends="compile,bindgen,bind" description="Main target"/>
<target name="compile" description="Compilation target">
<echo>Building file.</echo>
<!--相当于运行 javac命令进行源码编译-->
<javac srcdir="${basedir}/src/main/java/com/clover/frame/netty/http/xml/pojo/" destdir="${basedir}/target/classes" includeantruntime="true"/>
</target>
<target name="bindgen">
<echo message="Running BindGen tool"/>
<!--
相当于运行Java命令生成binding.xml文件 类似于网上说的如下命令 ->
java -cp ..libx-tools.jar ..BindGen -t 生成文件保存地址 -v 需要绑定文件的class文件 完整包名.类名
-->
<java classpathref="classpath" fork="true" failonerror="true" classname="org.jibx.binding.generator.BindGen">
<arg value="-s"/>
<arg value="src/main/java/"/>
<arg value="com.clover.frame.netty.http.xml.pojo.Order"/>
</java>
</target>
<target name="bind">
<!--将实体类的class和xml映射文件进行绑定-->
<echo message="Running bind"/>
<taskdef name="bind" classname="org.jibx.binding.ant.CompileTask">
<classpath refid="classpath"/>
</taskdef>
<bind binding="${basedir}/binding.xml">
<classpath refid="classpath"/>
</bind>
</target>
</project>
参考地址
https://blog.csdn.net/echohuangshihuxue/article/details/128653428