Java操作XML(10)--JiBX使用
Jibx是一款优秀的实现java对象和XML相互绑定的框架,功能类似JAXB。本文主要介绍Jibx的基本使用,文中所使用到的软件版本:Java 1.8.0_181、jibx 1.4.1。
1、JiBX简介
JiBX使用绑定文挡(binding definition document)来定义XML与Java对象转换的规则,这个文挡就是联系XML与Java对象之间的桥梁;详细说明可参考官网:http://jibx.sourceforge.net。
JiBX的使用过程分成两步:
第一步是Binding Compiler,这是一个前期准备过程,包括定义绑定文挡,定义与XML绑定在一起的Java对象,然后编译生成中间操作类。
第二步是Runtime,使用Binding Compiler编译好的中间操作类处理XML和Java对象的转换;如果需要修改映射需要重新生成中间类。
2、JiBX使用
2.1、引入依赖及JiBX的maven插件
<dependency> <groupId>org.jibx</groupId> <artifactId>jibx-run</artifactId> <version>1.3.3</version> </dependency> ... <build> ... <plugins> <plugin> <!-- 生成jibx class信息 --> <groupId>org.jibx</groupId> <artifactId>jibx-maven-plugin</artifactId> <version>1.3.3</version> <configuration> <schemaBindingDirectory>src/main/java</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>
2.2、xml样例文件
<?xml version="1.0" encoding="utf-8" ?> <school:grade xmlns:school="http://www.w3.org/TR/html4/school/"> <school:student rollno="1" age="11"> <school:firstname>cxx1</school:firstname> <school:lastname>Bob1</school:lastname> <school:nickname>stars1</school:nickname> <school:marks>85</school:marks> </school:student> <school:student rollno="2" age="12"> <school:firstname>cxx2</school:firstname> <school:lastname>Bob2</school:lastname> <school:nickname>stars2</school:nickname> <school:marks>85</school:marks> </school:student> <school:student rollno="3" age="13"> <school:firstname>cxx3</school:firstname> <school:lastname>Bob3</school:lastname> <school:nickname>stars3</school:nickname> <school:marks>85</school:marks> </school:student> </school:grade>
2.3、xml对应的类
2.3.1、班级类
package com.abc.demo.general.xml; import java.util.List; /** * 班级 */ public class Grade { private List<Student> students; public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } @Override public String toString() { return "Grade{" + "students=" + students + '}'; } }
2.3.2、学生类
package com.abc.demo.general.xml; /** * 学生 */ public class Student { private int rollno; private int age; private String firstname; private String lastname; private String nickname; private String marks; public Student() {} public Student(int rollno, int age, String firstname, String lastname, String nickname, String marks) { this.rollno = rollno; this.age = age; this.firstname = firstname; this.lastname = lastname; this.nickname = nickname; this.marks = marks; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getMarks() { return marks; } public void setMarks(String marks) { this.marks = marks; } @Override public String toString() { return "Student{" + "rollno=" + rollno + ", age=" + age + ", firstname='" + firstname + '\'' + ", lastname='" + lastname + '\'' + ", nickname='" + nickname + '\'' + ", marks='" + marks + '\'' + '}'; } }
2.4、绑定文档
绑定文档可以自己编写(绑定文档的规则介绍详见:http://jibx.sourceforge.net/binding/xml-summary.html),也可以通过JiBX提供的工具来生成:
java -cp ../../lib/jibx-tools.jar:bin org.jibx.binding.generator.BindGen org.jibx.starter1.Order
这里自己编写binding.xml:
<binding> <mapping name="grade" class="com.abc.demo.general.xml.Grade"> <namespace prefix="school" uri="http://www.w3.org/TR/html4/school/" default="elements"></namespace> <collection field="students" type="java.util.ArrayList" item-type="com.abc.demo.general.xml.Student"> </collection> </mapping> <mapping name="student" class="com.abc.demo.general.xml.Student"> <namespace prefix="school" uri="http://www.w3.org/TR/html4/school/" default="elements"></namespace> <value style="attribute" name="rollno" field="rollno"></value> <value style="attribute" name="age" field="age"></value> <value name="firstname" field="firstname"></value> <value name="lastname" field="lastname"></value> <value name="nickname" field="nickname"></value> <value name="marks" field="marks"></value> </mapping> </binding>
2.5、JiBX使用例子
2.5.1、Binding Compiler
使用binding.xml及对应的实体类编译生成中间操作类。
点击“compile”后会生成相应的绑定类:
2.5.1、Runtime
使用Binding Compiler编译好的中间操作类来处理O/X转换:Java对象转成xml以及xml转成Java对象。
package com.abc.demo.general.xml; import org.jibx.runtime.BindingDirectory; import org.jibx.runtime.IBindingFactory; import org.jibx.runtime.IMarshallingContext; import org.jibx.runtime.IUnmarshallingContext; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; /** * 使用JiBX处理xml */ public class JiBXCase { private static Logger logger = LoggerFactory.getLogger(JiBXCase.class); /** * java对象转成xml * @throws Exception */ @Test public void javaToXml() throws Exception { List<Student> students = new ArrayList<>(); Student student1 = new Student(1, 11,"cxx1", "Bob1", "stars1", "85"); Student student2 = new Student(2, 12, "cxx2", "Bob2", "stars2", "85"); Student student3 = new Student(3, 13, "cxx3", "Bob3", "stars3", "85"); students.add(student1); students.add(student2); students.add(student3); Grade grade = new Grade(); grade.setStudents(students); IBindingFactory factory = BindingDirectory.getFactory(Grade.class); IMarshallingContext marshallingContext = factory.createMarshallingContext(); marshallingContext.marshalDocument(grade, "utf-8", true, System.out); } /** * xml转成java对象 * @throws Exception */ @Test public void xmlToJava() throws Exception { IBindingFactory factory = BindingDirectory.getFactory(Grade.class); IUnmarshallingContext unmarshallingContext = factory.createUnmarshallingContext(); Grade grade = (Grade) unmarshallingContext.unmarshalDocument(JiBXCase.class.getResourceAsStream("student3.xml"), null); logger.info(grade.toString()); } }