簡介
JiBX is a tool for binding XML data to Java objects
這是官網(wǎng)開篇介紹,JiBX 是綁定XML結(jié)構(gòu)數(shù)據(jù)到Java對(duì)象上的工具,效率是Xstream的3倍。
使用方法
例子:
Java 對(duì)象
package com.zb;
public class JibxBean {
private String name;
private Integer age;
// 1 - 男;2 - 女
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
binding.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<binding value-style="element">
<mapping name="Root" class="com.zb.JibxBean" ordered="false">
<value name="Name" field="name" usage="optional" />
<value name="Age" field="age" usage="optional" />
<value name="Sex" field="sex" usage="optional" />
</mapping>
</binding>
XML結(jié)構(gòu)數(shù)據(jù)
<Root>
<Name>張三</Name>
<Age>18</Age>
<Sex>1</Sex>
</Root>
一 執(zhí)行綁定
/zb 目錄下存在 JibxBean.class 和 binding.xml 文件
方法一:
將Java對(duì)象的class類,與binding.xml文件放在同級(jí)目錄下,使用jibx-bind.jar進(jìn)行編譯綁定。
- 進(jìn)入 class 與 binding.xml 所在目錄 /zb
- 使用命令
java -jar /home/dennis/jibx/lib/jibx-bind.jar binding.xml進(jìn)行編譯綁定
官方翻譯:
一旦你有一個(gè)綁定規(guī)則,你需要使用 JiBX 綁定編譯,來將你的 class 文件和綁定規(guī)則進(jìn)行綁定。一個(gè)簡單方式是,使用提供的 jibx-bind.jar 包(在 /home/dennis/jibx/lib 目錄下)執(zhí)行綁定。例如,如果 JiBX 安裝在 /home/dennis,然后你程序的根目錄中有 binding.xml 文件,你只需要執(zhí)行:
java -jar /home/dennis/jibx/lib/jibx-bind.jar binding.xml
方法二:
自動(dòng)運(yùn)行當(dāng)前目錄下 class 文件和 jar 包中的類,進(jìn)行綁定。
例子:需要綁定的 class 文件在 a.jar 中,所在路徑:/zb/lib/a.jar
- 進(jìn)入 class 與 binding.xml 所在目錄 /zb
- 使用命令
java -cp .:lib/a.jar:/home/dennis/jibx/lib/jibx-bind.jar org.jibx.binding.Compile binding.xml進(jìn)行編譯綁定
官方翻譯:
當(dāng)你使用此方法進(jìn)行綁定時(shí),JiBX 將會(huì)自動(dòng)地包括當(dāng)前目錄中被加載的 class 文件。它將需要訪問所有在 binding.xml 文件中被綁定的類,因?yàn)橐ū唤壎惖母割悾孕枰渌念惵窂?,?jar 包到當(dāng)前執(zhí)行目錄中,但是不幸的是,直接執(zhí)行 jibx-bind.jar 進(jìn)行綁定就不會(huì)生效,你需要指定編譯綁定的類,即 org.jibx.binding.Compile,并且在你的當(dāng)前執(zhí)行目錄(classpath)中要包含 jibx-bind.jar。如下所示(在一行,顯示只有格式化):
java -cp .:lib/support.jar:/home/dennis/jibx/lib/jibx-bind.jar org.jibx.binding.Compile binding.xml
二 XML 結(jié)構(gòu)與 Java 對(duì)象轉(zhuǎn)換
XML 結(jié)構(gòu)轉(zhuǎn)換為 Java 對(duì)象
IBindingFactory bfact = BindingDirectory.getFactory(JibxBean.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object obj = uctx.unmarshalDocument(new FileInputStream("filename.xml"), null);
// 轉(zhuǎn)成JibxBean
JibxBean bean = (JibxBean) obj;
uctx.unmarshalDocument(),方法第一個(gè)參數(shù),需要是一個(gè)流,從XML結(jié)構(gòu)字符串轉(zhuǎn)化為流也可以
Java 對(duì)象轉(zhuǎn)換為 XML 結(jié)構(gòu)
JibxBean bean = new JibxBean();
bean.setName("李四");
bean.setAge(20);
bean.setSex(2);
StringWriter sw = new StringWriter();
IBindingFactory bfact = BindingDirectory.getFactory(JibxBean.class);
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.marshalDocument(bean, "UTF-8", null, sw);
String result = sw.toString();
System.out,println(result);
輸出結(jié)果:
<Root>
<Name>李四</Name>
<Age>20</Age>
<Sex>2</Sex>
</Root>
Eclipse 插件使用
添加 JiBX 插件下載地址
JiBX 官網(wǎng)提供的地址為:
http://jibx.sourceforge.net/eclipse/
-
點(diǎn)擊 Eclipse 工具欄中 Help -> Install New Software,按照下圖添加 JiBX 插件
-
選擇要安裝的插件版本
等待下載完畢
使用 JiBX 插件執(zhí)行綁定操作
-
選擇 JiBX 轉(zhuǎn)換 Java 對(duì)象所在的工程,點(diǎn)擊右鍵,選擇 Properties
按照下圖設(shè)置:- 選擇 JiBX 選項(xiàng)
- 設(shè)置 binding.xml 文件所在文件夾
-
選擇 JiBX 的版本(若安裝多個(gè),則選)
-
使 JiBX 插件生效
當(dāng) Java 對(duì)象編譯后 class 發(fā)生變化,或 binding.xml 文件綁定關(guān)系發(fā)生變化,JiBX 插件都會(huì)自動(dòng)重新執(zhí)行綁定操作
參考官網(wǎng)解釋:
A new Eclipse plugin for JiBX is now available. You can install the plugin by making use of Eclipse's Software Updates handling, with the update site url http://jibx.sourceforge.net/eclipse/. If you want a detailed walk-through of how to configure Eclipse to use this site, see the Install page.
Once you've installed the plugin, you can activate it for any of your Eclipse projects using the project context menu, and for each project specify a folder containing the binding definitions used by that project. The plugin will parse all XML files contained in your specified mappings folder to identify which Java classes require bindings, and will automatically run the JiBX binding compiler whenever it detects a change that might effect the bindings or bound classes. For a detailed walk-through of how to activate the plugin for a project, and details of when and how it runs the binding compiler, see the Usage page.



