Introduction to JiBX – JiBX简介

最后修改: 2017年 3月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

JiBX is a tool for binding XML data to Java objects. It provides solid performance compared to other common tools such as JAXB.

JiBX是一个用于将XML数据绑定到Java对象的工具。与JAXB等其他常用工具相比,它提供了坚实的性能。

JiBX is also quite flexible when compared to other Java-XML tools, using binding definitions to decouple the Java structure from XML representation so that each can be changed independently.

与其他Java-XML工具相比,JiBX也相当灵活,它使用绑定定义将Java结构与XML表示解耦,从而使两者可以独立改变。

In this article, we’ll explore the different ways provided by JiBX of binding the XML to Java objects.

在这篇文章中,我们将探讨JiBX所提供的将XML绑定到Java对象的不同方式。

2. Components of JiBX

2.JiBX的组成部分

2.1. Binding Definition Document

2.1.绑定定义文件

The binding definition document specifies how your Java objects are converted to or from XML.

绑定定义文档指定了你的Java对象如何转换为XML或从XML转换。

The JiBX binding compiler takes one or more binding definitions as input, along with actual class files. It compiles the binding definition into Java bytecode by adding it to the class files. Once the class files have been enhanced with this compiled binding definition code, they are ready to work with JiBX runtime.

JiBX 绑定编译器将一个或多个绑定定义以及实际的类文件作为输入。它通过将绑定定义添加到类文件中,将其编译成Java字节码。一旦类文件经过编译后的绑定定义代码的增强,它们就可以在JiBX运行时工作了。

2.2. Tools

2.2.工具

There are three main tools that we are going to use:

我们要使用的主要工具有三个。

  • BindGen – to generate the binding and matching schema definitions from Java code
  • CodeGen – to create the Java code and a binding definition from an XML schema
  • JiBX2Wsdl – to make the binding definition and a matching WSDL along with a schema definition from existing Java code

3. Maven Configuration

3.Maven配置

3.1. Dependencies

3.1.依赖性

We need to add the jibx-run dependency in the pom.xml:

我们需要在pom.xml中添加jibx-run的依赖。

<dependency>
    <groupId>org.jibx</groupId>
    <artifactId>jibx-run</artifactId>
    <version>1.3.1</version>
</dependency>

The latest version of this dependency can be found here.

该依赖性的最新版本可以在这里找到。

3.2. Plugins

3.2. 插件

To perform the different steps in JiBX like code generation or binding generation, we need to configure maven-jibx-plugin in pom.xml.

为了执行JiBX的不同步骤,如代码生成或绑定生成,我们需要在pom.xml中配置maven-jibx-plugin

For the case when we need to start from the Java code and generate the binding and schema definition, let’s configure the plugin:

对于我们需要从Java代码开始并生成绑定和模式定义的情况,让我们来配置这个插件。

<plugin>
    <groupId>org.jibx</groupId>
    <artifactId>maven-jibx-plugin</artifactId>
    ...
    <configuration>
        <directory>src/main/resources</directory>
        <includes>
            <includes>*-binding.xml</includes>
        </includes>
        <excludes>
            <exclude>template-binding.xml</exclude>
        </excludes>
        <verbose>true</verbose>
    </configuration>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>bind</goal>
            </goals>
        </execution>
    </executions>
</plugin>

When we have a schema and we generate the Java code and binding definition, the maven-jibx-plugin is configured with the information about schema file path and path to the source code directory:

当我们有了模式并生成Java代码和绑定定义时,maven-jibx-plugin会被配置为模式文件路径和源代码目录路径的信息。

<plugin>
    <groupId>org.jibx</groupId>
    <artifactId>maven-jibx-plugin</artifactId>
    ...
    <executions>
        <execution>
        <id>generate-java-code-from-schema</id>
        <goals>
             <goal>schema-codegen</goal>
        </goals>
            <configuration>
                <directory>src/main/jibx</directory>
                <includes>
                    <include>customer-schema.xsd</include>
                </includes>
                <verbose>true</verbose>
            </configuration>
            </execution>
            <execution>
                <id>compile-binding</id>
                <goals>
                    <goal>bind</goal>
                </goals>
            <configuration>
                <directory>target/generated-sources</directory>
                <load>true</load>
                <validate>true</validate>
                <verify>true</verify>
            </configuration>
        </execution>
    </executions>
</plugin>

4. Binding Definitions

4.约束力的定义

Binding definitions are the core part of JiBX. A basic binding file specifies the mapping between XML and Java object fields:

绑定定义是 JiBX 的核心部分。一个基本的绑定文件指定了XML和Java对象字段之间的映射关系。

<binding>
    <mapping name="customer" class="com.baeldung.xml.jibx.Customer">
        ...
        <value name="city" field="city" />
    </mapping>
</binding>

4.1. Structure Mapping

4.1.结构映射

Structure mapping makes the XML structure look similar to object structure:

结构映射使XML结构看起来与对象结构相似。

<binding>
    <mapping name="customer" class="com.baeldung.xml.jibx.Customer">
    ...
    <structure name="person" field="person">
        ...
        <value name="last-name" field="lastName" />
    </structure>
    ...    
    </mapping>
</binding>

The corresponding classes for this structure are going to be:

这个结构对应的类将是。

public class Customer {
    
    private Person person;
    ...
    
    // standard getters and setters

}

public class Person {
    
    private String lastName;
    ...
    
    // standard getters and setters

}

4.2. Collection and Array Mappings

4.2.集合数组映射

JiBX binding provides an easy way for working with a collection of objects:

JiBX绑定提供了一种简单的方法来处理对象的集合。

<mapping class="com.baeldung.xml.jibx.Order" name="Order">
    <collection get-method="getAddressList" 
      set-method="setAddressList" usage="optional" 
      createtype="java.util.ArrayList">
        
        <structure type="com.baeldung.xml.jibx.Order$Address" 
          name="Address">
            <value style="element" name="Name" 
              get-method="getName" set-method="setName"/>
              ...
        </structure>
     ...
</mapping>

Let’s see corresponding mapping Java objects:

让我们看看相应的Java对象的映射。

public class Order {
    List<Address> addressList = new ArrayList<>();
    ...
 
    // getters and setters here
}

public static class Address {
    private String name;
    
    ...
    // standard getters and setter
    
}

4.3. Advanced Mappings

4.3.高级映射

So far we have seen a basic mapping definition. JiBX mapping provides different flavors of mapping like abstract mapping and mapping inheritance.

到目前为止,我们已经看到了一个基本的映射定义。JiBX映射提供了不同类型的映射,如抽象映射和映射继承。

Let see how can we define an abstract mapping:

让我们看看如何定义一个抽象的映射。

<binding>
    <mapping name="customer" 
      class="com.baeldung.xml.jibx.Customer">

        <structure name="person" field="person">
            ...
            <value name="name" field="name" />
        </structure>
        <structure name="home-phone" field="homePhone" />
        <structure name="office-phone" field="officePhone" />
        <value name="city" field="city" />
    </mapping>
 
    <mapping name="phone" 
      class="com.baeldung.xml.jibx.Phone" abstract="true">
        <value name="number" field="number"/>
    </mapping>
</binding>

Let’s see how this binds to Java objects:

让我们看看这如何与Java对象绑定。

public class Customer {
    private Person person;
    ...
    private Phone homePhone;
    private Phone officePhone;
    
    // standard getters and setters
    
}

Here we have specified multiple Phone fields in Customer class. The Phone itself is again a POJO:

这里我们在Customer类中指定了多个Phone字段。Phone本身也是一个POJO。

public class Phone {

    private String number;
    
    // standard getters and setters
}

In addition to regular mappings, we can also define extensions. Each extension mapping refers to some base mapping. At the time of marshaling, the actual object type decides which XML mapping is applied.

除了常规映射之外,我们还可以定义扩展。每个扩展映射都指的是一些基础映射。在编排时,实际的对象类型决定了哪种XML映射被应用。

Let’s see how the extensions work:

让我们看看这些扩展是如何工作的。

<binding>
    <mapping class="com.baeldung.xml.jibx.Identity" 
      abstract="true">
        <value name="customer-id" field="customerId"/>
    </mapping>
    ...  
    <mapping name="person" 
      class="com.baeldung.xml.jibx.Person" 
      extends="com.baeldung.xml.jibx.Identity">
        <structure map-as="com.baeldung.xml.jibx.Identity"/>
        ...
    </mapping>
    ...
</binding>

Let’s look at the corresponding Java objects:

让我们看一下相应的Java对象。

public class Identity {

    private long customerId;
    
    // standard getters and setters
}

5. Conclusion

5.结论

In this quick article, we have explored different ways by which we can use the JiBX for converting XML to/from Java objects. We have also seen how we can make use binding definitions to work with different representations.

在这篇文章中,我们已经探索了不同的方法,我们可以使用JiBX将XML转换为/从Java对象。我们也看到了我们如何利用绑定定义来处理不同的表现形式。

Full code for this article is available over on GitHub.

本文的完整代码可在GitHub上获得