Jibx插件的使用

简介: Jibx是一款非常优秀的XML文件数据绑定的框架,提供灵活的绑定映射文件,实现数据对象和XML文件之间的转换,并不需要修改既有的Java,另外,它的转换效率是目前很多其他开源项目都无法比拟的。本文来演示下如何使用


 Jibx是一款非常优秀的XML文件数据绑定的框架,提供灵活的绑定映射文件,实现数据对象和XML文件之间的转换,并不需要修改既有的Java,另外,它的转换效率是目前很多其他开源项目都无法比拟的。本文来演示下如何使用

Jibx插件的使用

下载Jibx插件:

链接:https://pan.baidu.com/s/1Va9D8LZlxoVU5VndC7T1ag

提取码:oyjt

image.png

Pojo创建

Order类

image.png

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 + "]";
    }
}

生成绑定文件

 我们想将绑定文件生成在src/main/resources/jibx目录下,进入cmd下,切换到maven项目的target/classes目录下

image.png

然后执行如下命令

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文件 完整包名.类名

image.png

对字节码增强

方式1:jibx-bind.jar增强

未增强前:

image.png

执行如下命令:

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

image.png

image.png

方式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文件

image.png

测试

 执行如下测试代码

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);
  }
}

image.png

XML的序列化和反序列执行成功。


相关文章
|
4月前
|
人工智能 自然语言处理 Java
SemanticKernel:添加插件
SemanticKernel:添加插件
79 0
SemanticKernel:添加插件
|
6月前
|
存储 安全 数据库
Duplicator插件
【6月更文挑战第5天】Duplicator插件
55 2
|
SQL 人工智能 JSON
插件
待确定推荐 • Grazie Professional
107 0
|
开发工具
ideaVim插件的使用
ideaVim插件的使用
120 0
【3款文献阅读的插件】
【3款文献阅读的插件】
739 0
|
文字识别 数据安全/隐私保护
大漠插件7.2302
大漠插件是一款文字图片识别的编程插件.能够被大多主流编语言调用
989 0
大漠插件7.2302
|
Rust Java Linux
优秀插件
优秀插件
207 0
优秀插件
|
JavaScript 前端开发 API
|
XML 大数据 数据格式
ExcelDataReader插件的使用
ExcelDataReader插件的使用
362 0
|
XML 人工智能 JSON
我个人中意的VS2017/VS2019插件,推荐给大家(#^.^#)(2)
我个人中意的VS2017/VS2019插件,推荐给大家(#^.^#)
1004 0
我个人中意的VS2017/VS2019插件,推荐给大家(#^.^#)(2)