大概
官方首页 传送门
jibx是一个xml与java 对象之间进行转换的框架,听说效率是很高的,spring中也有使用。
在学习netty的http+xml协议过程中,使用到它,无奈知识浅薄,期间不幸遇到了很多麻烦,在此记录一下从java类到xml的使用,以及相关配置,后备无患。关于原理,更详细的用法,或者其他用法,比如从xml到类的使用,得去官网慢慢看了。
步骤
一种详细使用方式。工具为idea+maven+jdk8。
导入相关包pom.xml
<!-- https://mvnrepository.com/artifact/org.jibx/jibx-run -->
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-run</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jibx/jibx-extras -->
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-extras</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jibx/jibx-tools -->
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-tools</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jibx/jibx-bind -->
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-bind</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jibx/jibx-schema -->
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-schema</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
配置maven插件
插件的参数,官网上有,这个项目是直接用的springboot项目结构。
includeSchemaBindings 参数指定的是我们生成的binding文件名,下面的配置说告诉插件src/main/resources目录下*-binding.xml文件就是binding文件。
<plugin>
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.3.3</version>
<configuration>
<schemaBindingDirectory>src/main/resources</schemaBindingDirectory>
<includeSchemaBindings>
<includeSchemaBindings>*-binding.xml</includeSchemaBindings>
</includeSchemaBindings>
<excludeSchemaBindings>
<excludeSchemaBinding>template-binding.xml</excludeSchemaBinding>
</excludeSchemaBindings>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
编写类文件
getter setter toString 方法未写,属性是基本类型用包装类
public class Order implements Serializable {
private String number;
private Date date;
private Customer customer;
}
public class Customer implements Serializable {
private String name;
private Integer age;
}
类文件编译
有3种方式,
第一种手动的javac,
javac也有很多要求,不注意就是文件找不到。
先cd到项目classpath目录下,再执行下面的代码,注意根据自己的项目路径修改。
javac -cp . we/us/nettystudy/codec/jibx/Order.java
第二种,直接用idea的build吧,
第三种,用maven插件complier。
生成binding.xml和xsd文件
基于上一步骤的第二种方式,
首先得先将jibx的相关jar统一放到一个目录下,可以maven仓库中提取,或者直接去官网下载。下面的jibx-tools.jar 用于生成文件,
然后cd到项目编译后的classes目录下,再执行下面的代码,注意根据自己的项目路径修改,参数可在官网上查询或者直接不带任何参数运行,会有参数说明打印出来。
java -cp D:\idea\fn\jibx\lib\jibx-tools.jar org.jibx.binding.generator.BindGen -b order-binding.xml we.us.nettystudy.codec.jibx.Order
生成的order-binding.xml文件
<binding xmlns:ns1="http://we.us/nettystudy/codec/jibx" name="order_binding" package="we.us.nettystudy.codec.jibx">
<namespace uri="http://we.us/nettystudy/codec/jibx" default="elements"/>
<mapping abstract="true" type-name="ns1:order" class="we.us.nettystudy.codec.jibx.Order">
<value style="element" name="number" field="number" usage="optional"/>
<value style="attribute" name="date" field="date" usage="optional"/>
<structure field="customer" usage="optional" name="customer">
<value style="element" name="name" field="name" usage="optional"/>
<value style="attribute" name="age" field="age" usage="optional"/>
</structure>
</mapping>
<mapping class="we.us.nettystudy.codec.jibx.Order" name="order">
<structure map-as="ns1:order"/>
</mapping>
</binding>
生成的jibx.xsd文件
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://we.us/nettystudy/codec/jibx" elementFormDefault="qualified" targetNamespace="http://we.us/nettystudy/codec/jibx">
<xs:complexType name="order">
<xs:sequence>
<xs:element type="xs:string" name="number" minOccurs="0"/>
<xs:element name="customer" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name" minOccurs="0"/>
</xs:sequence>
<xs:attribute type="xs:int" name="age"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:dateTime" name="date"/>
</xs:complexType>
<xs:element type="tns:order" name="order"/>
</xs:schema>
执行bind操作
在执行bind之前,执行一次maven的complier操作一次,然后找到jibx的maven插件,调用一下其中的bind操作就ok
编写测试代码
测试数据orderdata.xml,将其放在项目classpath目录下,springboot项目,直接扔到resource文件下即可。
orderdata.xml
<?xml version="1.0" encoding="utf-8" ?>
<order xmlns="http://we.us/nettystudy/codec/jibx" date="2002-05-30T09:30:10.5">
<number>1234</number>
<customer age="18">
<name>哦哦哦</name>
</customer>
</order>
java测试代码
/**
* 使用jibx解码为对象
*/
public static void un() {
try {
IBindingFactory factory = BindingDirectory.getFactory(Order.class);
IUnmarshallingContext unmarshallingContext = factory.createUnmarshallingContext();
InputStream resourceAsStream = Order.class.getResourceAsStream("/orderdata.xml");
Order o = (Order) unmarshallingContext.unmarshalDocument(resourceAsStream, null);
System.out.println(o);
} catch (JiBXException e) {
e.printStackTrace();
}
}
/**
* 使用jibx 编码为xml
*/
public static void ma() {
try {
Customer customer = new Customer();
customer.setAge(55);
customer.setName("吖吖");
Order order = new Order();
order.setCustomer(customer);
order.setNumber("124665");
order.setDate(new Date());
IBindingFactory factory = BindingDirectory.getFactory(Order.class);
IMarshallingContext marshallingContext = factory.createMarshallingContext();
StringWriter stringWriter = new StringWriter();
marshallingContext.marshalDocument(order, null, true, stringWriter);
System.out.println(stringWriter.getBuffer());
} catch (JiBXException e) {
e.printStackTrace();
}
}
测试结果
最后
官网全是英文。不着急,慢慢看,就算英文看的慢,只要看的久,总能琢磨点东西出来。