天天看點

Jibx插件的使用

 Jibx是一款非常優秀的XML檔案資料綁定的架構,提供靈活的綁定映射檔案,實作資料對象和XML檔案之間的轉換,并不需要修改既有的Java,另外,它的轉換效率是目前很多其他開源項目都無法比拟的。本文來示範下如何使用

Jibx插件的使用

下載下傳Jibx插件:

連結:

https://pan.baidu.com/s/1Va9D8LZlxoVU5VndC7T1ag 提取碼:oyjt
Jibx插件的使用

Pojo建立

Order類

Jibx插件的使用

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目錄下

Jibx插件的使用

然後執行如下指令

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檔案 完整包名.類名      
Jibx插件的使用

對位元組碼增強

方式1:jibx-bind.jar增強

未增強前:

Jibx插件的使用

執行如下指令:

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      
Jibx插件的使用
Jibx插件的使用

方式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檔案

Jibx插件的使用

測試

 執行如下測試代碼

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);
    }
}
      
Jibx插件的使用

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