-
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()); } }
来源:https://www.cnblogs.com/wuyongyin/p/14343582.html
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比