##注意事项
JiBX:1.2.2 (https://sourceforge.net/projects/jibx/files/jibx/jibx-1.2.2/)
JDK:1.6 (http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html)
ANT:1.9.12 (http://mirrors.shu.edu.cn/apache//ant/binaries/)
版本不一定要是以上的,但是其他的可能会报各种莫名其妙的错误
##重要步骤
###环境变量
###项目结构
###执行命令及变化 执行的目录:
ant compile bindgen
###另一种生成xml的方法 参考:http://www.voidcn.com/article/p-gdeaekhv-kw.html
编译Java类文件,生成class文件
进入class文件目录
执行:
java -cp E:\WORK\jibx_1_2_2\jibx\lib\jibx-tools.jar;bin org.jibx.binding.generator.BindGen Address
##一个简单的例子
其中jdk是1.6 lib是jibx的jar
package com.xh.bean;
public class Student {
private String name;
private String SNO;
private int classNO;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSNO() {
return SNO;
}
public void setSNO(String SNO) {
this.SNO = SNO;
}
public int getClassNO() {
return classNO;
}
public void setClassNO(int classNO) {
this.classNO = classNO;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", SNO='" + SNO + '\'' +
", classNO=" + classNO +
", age=" + age +
'}';
}
}
import com.xh.bean.Student;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.JiBXException;
import java.io.StringWriter;
public class Main {
public static void main(String[] args) throws JiBXException {
Student student=new Student();
student.setAge(12);
student.setClassNO(111);
student.setName("jon");
student.setSNO("NO_ewewewj3413");
IBindingFactory factory = BindingDirectory.getFactory(Student.class);
IMarshallingContext mctx= factory.createMarshallingContext();
StringWriter sw=new StringWriter();
mctx.setIndent(2);
mctx.marshalDocument(student, "UTF-8", null, sw);
System.out.printf(String.valueOf(sw));
}
}
如果直接运行测试类:
就会报错:
Exception in thread "main" Unable to access binding information for class com.xh.bean.Student
Make sure the binding has been compiled
java.lang.NoSuchFieldException: JiBX_bindingList
###重要的步骤:
编译项目 如果执行了测试,这一步可以跳过。
进入编译后的class根目录:
生成bind.xml,注意完整类名
java -cp bin;D:\ide\jibx_1_2_2\jibx\lib\jibx-tools.jar org.jibx.binding.generator.BindGen -b bind.xml -s src com.xh.bean.Student
绑定xml
java -cp bin;D:\ide\jibx_1_2_2\jibx\lib\jibx-bind.jar org.jibx.binding.Compile -v bind.xml
再次运行测试:
<?xml version="1.0" encoding="UTF-8"?>
<student xmlns="http://xh.com/bean" classNO="111" age="12">
<name>jon</name>
<SNO>NO_ewewewj3413</SNO>
</student>
##相关资料