JiBX 的简单使用

在学习 Netty 的过程中,有使用 JiBX 的地方,所以这里简单记录一下使用方法。

JiBX 简介

JiBX 是一个绑定 XML 数据到 Java 对象的框架。JiBX 用一个绑定定义文挡(binding definition document)来定义 XML 数据与 Java 对象转换的规则,这个文挡就是联系 XML 数据与 Java 对象之间的桥梁。

这里有必要先介绍两个数据绑定术语 marshal 和 unmarshal,marshal 是由 Java 对象生成 XML 文挡,unmarshal 是根据 XML 文挡建立 Java 对象。

使用 JiBX 的过程分成两个过程,一个是 binding compiler,另一个是 binding runtime。binding compiler 是一个前期准备过程,包括定义绑定定义文挡,定义与 XML 绑定在一起的 Java 对象,然后编译。在这个过程,JiBX 比其他项目在操作上要简单,不用定义 DTD 和 Schema,缺点是需要自己定义 Java 程序。binding runtime 是使用 binding compiler 编译好的 Java class 处理 XML 数据。

JiBX 也用到了第三方的工具 XPP3 Pull Parser 和 BCEL,在 JiBX 发布的文件中也包含了这两个工具相关的文件。

首先看一下参数(这是从源码里扒出来的):
Usage: java org.jibx.binding.Compile [-b] [-l] [-v] binding1 binding2 ...
where:
 -b  turns on BCEL verification (debug option),
 -l  turns on test loading of modified or generated classes for validation, and
 -v  turns on verbose output
The bindingn files are different bindings to be compiled.


Usage: java org.jibx.binding.generator.BindGen [options] class1 class2 ...
where options are:
 -a       force abstract mappings for specified classes
 -b name  generated root binding name (default is 'binding.xml')
 -c path  input customizations file
 -m       force concrete mappings for specified classes
 -n uri=name,... schema namespace URI and file name pairs (default generates
          file names from URIs)
 -o       binding generation only flag, skip schema generation
 -p path,... class loading paths
 -s path,... source paths
 -t path  target directory for generated output (default is current directory)
 -v       verbose output flag
 -w       wipe all existing files from generation directory (ignored if current
          directory)
The class# files are different classes to be included in the binding (references
from these classes will also be included).
使用方法(生成并编译):
package com.phei.netty.protocol.http.xml;

import java.io.IOException;

import org.jibx.binding.Compile;
import org.jibx.binding.generator.BindGen;
import org.jibx.runtime.JiBXException;

public class GenBindFileTool {

    public static void main(String[] args) throws JiBXException, IOException {

        //genBindFiles();
        compile();
    }

    private static void compile() {
        String[] args = new String[2];

        // 打印生成过程的详细信息。可选
        args[0] = "-v";

        // 指定 binding 和 schema 文件的路径。必须
        args[1] = "./src/main/java/com/phei/netty/protocol/http/xml/pojo/order/binding.xml";

        Compile.main(args);
    }

    private static void genBindFiles() throws JiBXException, IOException {
        String[] args = new String[9];

        // 指定pojo源码路径(指定父包也是可以的)。必须
        args[0] = "-s";
        args[1] = "src";

        // 自定义生成的binding文件名,默认文件名binding.xml。可选
        args[2] = "-b";
        args[3] = "binding.xml";

        // 打印生成过程的一些信息。可选
        args[4] = "-v";

        // 如果目录已经存在,就删除目录。可选
        args[5] = "-w";

        // 指定输出路径。默认路径 .(当前目录,即根目录)。可选
        args[6] = "-t";
        args[7] = "./src/main/java/com/phei/netty/protocol/http/xml/pojo/order";

        // 告诉 BindGen 使用下面的类作为 root 生成 binding 和 schema。必须
        args[8] = "com.phei.netty.protocol.http.xml.pojo.Order";

        BindGen.main(args);
    }
}
测试转换:
package com.phei.netty.protocol.http.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.phei.netty.protocol.http.xml.pojo.Order;
import com.phei.netty.protocol.http.xml.pojo.OrderFactory;

/**
 * @author Lilinfeng
 * @date 2014年3月1日
 * @version 1.0
 */
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 order = OrderFactory.create(123);
        // 转换成 xml
        String body = test.encode2Xml(order);
        // 转换回对象
        Order order2 = test.decode2Order(body);

        System.out.println(order2);
    }
}

最后提供一下源码的下载路径:https://gitee.com/liupeifeng3514/netty_test
在包com.phei.netty.protocol.http.xml

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值