jibx
Welcome to JiBX tutorial. JiBX is a very powerful framework for converting XML data to java object and vice versa. It is very useful in applications integration where XML is the format for data transfer. For example, Web Services and Legacy Systems Integration based on Message Oriented Model.
欢迎来到JiBX教程。 JiBX是一个非常强大的框架,用于将XML数据转换为Java对象,反之亦然。 在XML是数据传输格式的应用程序集成中,它非常有用。 例如,基于消息模型的 Web服务和旧系统集成。
Converting Java Object to XML is called Marshalling and creating java object from XML is called unmarshalling.
将Java对象转换为XML称为编组 ,从XML创建Java对象称为解组 。
There are many frameworks available for XML transformation such as JAXB and XMLBeans but JiBX differs in the approach for XML binding and transformation process.
有许多可用于XML转换的框架,例如JAXB和XMLBeans,但是JiBX在XML绑定和转换过程的方法上有所不同。
JiBX performs these tasks via utility classes generated at compile time via ant scripts. This approach reduces the processing time by moving away from the traditional two-step process with other parsers to a single step.
JiBX通过在编译时通过ant脚本生成的实用程序类来执行这些任务。 这种方法通过将与其他解析器一起使用的传统两步处理转移到一个步骤,从而减少了处理时间。
Benchmarks done on various XML binding tools have shown JiBX as the fastest and the most memory efficient parsing framework till date.
迄今为止,在各种XML绑定工具上进行的基准测试表明,JiBX是最快,最高效的内存解析框架。
Here in JiBX tutorial, I am providing a sample application for XML transformation using JiBX parser.
在JiBX教程的此处,我提供了一个使用JiBX解析器进行XML转换的示例应用程序。
System Information:
系统信息 :
bcel.jar and jibx-bind.jar are used for creating utility classes for XML transformation and jibx-run.jar is required for runtime environment.
bcel.jar和jibx-bind.jar用于创建XML转换的实用程序类,运行时环境需要jibx-run.jar。
You can download these jars or use maven pom.xml to download it and then rename it.
您可以下载这些jar或使用maven pom.xml进行下载,然后重命名。
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-bind</artifactId>
<version>1.3.1</version>
</dependency>
Let’s say we have an Employee class with three parameters – id, name, and hire date.
假设我们有一个Employee类,带有三个参数-id,name和雇用日期。
package com.journaldev.jibx.bean;
public class Employee {
private String id;
private String name;
private String hireDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHireDate() {
return hireDate;
}
public void setHireDate(String hireDate) {
this.hireDate = hireDate;
}
}
We need to convert this object to XML like this:
我们需要将此对象转换为XML,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Employee id="237871">
<name>Cisco</name>
<hiredate>Jan 03, 2011</hiredate>
</Employee>
For working with JiBX, we need to set up four things.
为了使用JiBX,我们需要设置四件事。
binding.xml
) that is used by the JiBX binding compiler to generate the binding definition in byte code i.e. class files JiBX绑定编译器用于以字节码(即类文件)生成绑定定义的映射定义文件( binding.xml
) jibx-bind.jar
and bcel.jar
, which are present in the distribution package. jibx-bind.jar
和bcel.jar
存在的JiBX编译器,它们在分发软件包中。 build.xml
) that is used to run the JiBX compiler and generate binding classes. 用于运行JiBX编译器并生成绑定类的Ant构建文件( build.xml
)。 jibx-run.jar
and should be present in the classpath at runtime. jibx-run.jar
存在的JiBX运行时,应在运行时出现在类路径中。 Below image shows the final eclipse project that we will have after everything is set up properly.
下图显示了在正确设置所有内容后我们将拥有的最终Eclipse项目。
For above transformation binding.xml
file will look like:
对于上述转换, binding.xml
文件将如下所示:
<binding>
<mapping name="Employee" class="com.journaldev.jibx.bean.Employee" ordered="false">
<value style="attribute" name="id" field="id" usage="optional" />
<value name="name" field="name" />
<value name="hiredate" field="hireDate" usage="optional" />
</mapping>
</binding>
The mappings are present in the root binding tag. All mappings for a particular class will be contained in a mapping tag.
映射存在于根绑定标记中。 特定类的所有映射将包含在映射标记中。
The name attribute in the mapping specifies the name of the node in the XML. In our case, it’s “Employee” and is linked to the Employee class by the class attribute.
映射中的name属性指定XML中节点的名称。 在我们的例子中,它是“ Employee”,并通过class属性链接到Employee类。
The optional attribute ordered=”false” removes the strict check in the XML for following the order as appearing in the binding.xml file. This means that the name tag can appear above or below hiredate
tag.
可选属性ordered =“ false”将删除XML中严格的检查,以遵循在binding.xml文件中出现的顺序。 这意味着名称标签可以出现在hiredate
标签上方或下方。
The elements of employee object are contained in value tags. The style=”attribute” specifies that the employee id should be an attribute of the employee tag.
雇员对象的元素包含在值标签中。 style =“ attribute”指定职员ID应该是职员标签的属性。
The field attribute is used to map the id element to the java variable id. An optional attribute usage=”optional” can be used to specify that the id attribute is not a mandatory one and may be skipped if not present in the XML, or is null in the Java object.
field属性用于将id元素映射到java变量id。 可选属性用法=“可选”可用于指定id属性不是强制性属性,如果XML中不存在id属性,或者在Java对象中为null,则可以跳过该属性。
Similarly other elements can be kept in normal value tags. The name attribute in the value tags specifies the name of the tag in the XML.
同样,其他元素也可以保留在正常值标签中。 值标签中的name属性指定XML中标签的名称。
For simplicity purpose and ease of understanding, the example is restricted to only these simple tags. Nested tags like structures, collections exist. More info on these can be found in the JiBX documentation.
为了简单起见和易于理解,该示例仅限于这些简单标签。 存在嵌套标签,如结构,集合。 有关这些的更多信息,请参见JiBX文档。
Once the mapping is done, we need to create the ant build file. For our example, it will look like this:
映射完成后,我们需要创建ant构建文件。 对于我们的示例,它将如下所示:
<?xml version="1.0"?>
<!-- ===================================================================
Ant build file for JiBX data binding starter example.
=================================================================== -->
<project basedir="." default="bind">
<!-- The following block is intended to set the jibx-home location. It first
checks the relative location of the JiBX libraries when this starter example
is run directly from the JiBX distribution, then (if that fails), looks for
an environmental variable JIBX_HOME with the installation path. If you prefer
to just set the path directly in this file, uncomment the following line and
set the value to the appropriate directory, then delete the rest of the Ant
commands down to the end of this block. -->
<property name="jibx-home" value="${basedir}"/>
<!-- End of jibx-home location setting block. -->
<!-- make sure required jars are present -->
<condition property="runtime-jars-found">
<available file="${basedir}/lib/jibx-run.jar"/>
</condition>
<condition property="binding-jars-found">
<and>
<available file="${basedir}/lib/bcel.jar"/>
<available file="${basedir}/lib/jibx-bind.jar"/>
<available file="${basedir}/lib/jibx-run.jar"/>
</and>
</condition>
<!-- set classpath for compiling and running application with JiBX -->
<path id="classpath">
<fileset dir="${basedir}/lib" includes="*.jar"/>
<pathelement location="${basedir}/bin"/>
</path>
<!-- make sure runtime jars are present -->
<target name="check-runtime">
<fail unless="jibx-home">JiBX home directory not found - define JIBX_HOME system property or set path directly in build.xml file.</fail>
<fail unless="runtime-jars-found">Required JiBX runtime jar jibx-run.jar was not found in JiBX home lib directory (${jibx-home}/lib)</fail>
</target>
<!-- make sure binding jars are present -->
<target name="check-binding" depends="check-runtime">
<fail unless="binding-jars-found">Required JiBX binding jar jibx-bind.jar or bcel.jar was not found in JiBX home lib directory (${jibx-home}/lib)</fail>
</target>
<!-- bind as a separate step -->
<target name="bind" depends="check-binding">
<echo message="Running JiBX binding compiler"/>
<taskdef name="bind" classname="org.jibx.binding.ant.CompileTask">
<classpath>
<fileset dir="${jibx-home}/lib" includes="*.jar"/>
</classpath>
</taskdef>
<bind binding="${basedir}/binding.xml">
<classpath refid="classpath"/>
</bind>
</target>
</project>
Copy binding file and build file in the root of JiBXTest project and do a build with the “bind” target specified. This will run the JiBX binding compiler that in turn takes the binding.xml mapping file and compiles it to the binding definition classes. These classes will be used by the JiBX runtime for working with XML.
在JiBXTest项目的根目录中复制绑定文件和构建文件,并使用指定的“ bind”目标进行构建。 这将运行JiBX绑定编译器,该编译器反过来获取binding.xml映射文件并将其编译为绑定定义类。 JiBX运行时将使用这些类来处理XML。
Pankaj:JIBXTest Pankaj$ pwd
/Users/Pankaj/EclipseWorkspace/JIBXTest
Pankaj:JIBXTest Pankaj$ ant bind
Buildfile: build.xml
check-runtime:
check-binding:
bind:
[echo] Running JiBX binding compiler
BUILD SUCCESSFUL
Total time: 0 seconds
Pankaj:JIBXTest Pankaj$ ls -ltr bin/com/journaldev/jibx/bean/
total 32
-rw-r--r-- 1 Pankaj staff 2507 Dec 14 18:10 JiBX_bindingEmployee_access2.class
-rw-r--r-- 1 Pankaj staff 3860 Dec 16 10:14 Employee.class
-rw-r--r-- 1 Pankaj staff 1663 Dec 16 10:56 JiBX_bindingFactory.class
-rw-r--r-- 1 Pankaj staff 2505 Dec 16 10:56 JiBX_bindingEmployee_access.class
Now we will test our binding. For this, we will use a sample program for marshalling an Employee java object to XML and un-marshalling XML string to the Employee object.
现在,我们将测试绑定。 为此,我们将使用一个示例程序将Employee java对象编组为XML,并将XML字符串解编组为Employee对象。
package com.journaldev.jibx.test;
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.journaldev.jibx.bean.Employee;
public class JibxTest {
public String marshalEmployee(Employee employee){
try {
IBindingFactory bfact = BindingDirectory.getFactory(Employee.class);
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
StringWriter stringWriter = new StringWriter();
mctx.setOutput(stringWriter);
mctx.marshalDocument(employee, "UTF-8", null);
String output = stringWriter.toString();
return output;
} catch (JiBXException e) {
e.printStackTrace();
}
return null;
}
public void unMarshalEmployee(String inputXml){
try {
IBindingFactory bfact = BindingDirectory.getFactory(Employee.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
StringReader stringReader = new StringReader(inputXml);
Employee employee = (Employee) uctx.unmarshalDocument(stringReader, null);
System.out.println("Employee ID:"+employee.getId());
} catch (JiBXException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
String inputXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Employee id=\"237871\"><name>Cisco</name><hiredate>Jan 03, 2011</hiredate></Employee>";
JibxTest jibxTest = new JibxTest();
jibxTest.unMarshalEmployee(inputXml);
Employee employee = new Employee();
employee.setId("237871");
employee.setName("Cisco");
employee.setHireDate("Jan 03, 2011");
System.out.println("Employee as XML String:"+jibxTest.marshalEmployee(employee));
}
}
Now we will run this test program from command line and check the output.
现在,我们将从命令行运行此测试程序并检查输出。
Pankaj:JIBXTest Pankaj$ cd bin
Pankaj:bin Pankaj$ java -cp .:../lib/jibx-run.jar com/journaldev/jibx/test/JibxTest
Employee ID:12345
Employee as XML String:<?xml version="1.0" encoding="UTF-8"?>
<Employee id="237871">
<name>Cisco</name>
<hiredate>Jan 03, 2011</hiredate>
</Employee>
Pankaj:bin Pankaj$
As you can see that we need to put only jibx runtime jar in the classpath to execute our test program.
如您所见,我们只需要在类路径中放入jibx运行时jar即可执行我们的测试程序。
I hope that the JiBX example given here is good for getting started with JiBX parser. You can download the JiBX tutorial project from below link.
我希望这里给出的JiBX示例可以很好地开始使用JiBX解析器。 您可以从下面的链接下载JiBX教程项目。
Reference: Official Website
参考: 官方网站
jibx