|  | 处理 XML 文档
在本节中,您将了解如何在运行时运行 JiBX 绑定编译器和使用 JiBX,从而处理 XML 文档。
运行 JiBX 绑定编译器
要在处理 XML 文档时使用生成的绑定定义,首先需要运行 JiBX 绑定编译器。按照绑定定义的指定,绑定编译器将把字节码添加到编译后的类文件,这些文件实际实现了与 XML 之间的来回转换。每次重新编译 Java 类或修改绑定定义时,都必须运行绑定编译器,因此一般最好把绑定编译器步骤添加到项目的标准构建流程中。
jibx-bind.jar 中的 JiBX 发行版附带了绑定编译器。JiBX 文档将提供通过各种方法运行绑定编译器的完整信息,包括如何在运行应用程序时(而非在构建时)调用绑定编译器。JiBX 还提供了 Eclipse 和 IntelliJ IDEA 的插件,这样在使用这些 IDE 时将自动运行绑定编译器。
根据本教程的目的,您将把一切简单化并且只通过 Ant 运行绑定编译器。build.xml 的 compile 目标将通过编译生成的代码和提供的测试程序来为绑定做准备,而 bind 目标实际运行绑定编译器。假定您已经运行了 codegen 目标,图 2 将显示运行这些目标时应当会看到的输出(您还可以通过在命令行中按顺序列出这些目标来运行全部三个目标:ant codegen compile bind)。
图 2. Ant 构建 compile 和 bind 任务
在运行时使用 JiBX
清单 3 显示了匹配模式的简单测试文档,包含在教程的代码下载中,名为 starter.xml:
清单 3. 订单模式的测试文档
<order orderDate="2008-10-18" shipDate="2008-10-22" xmlns="http://jibx.org/starter">
<orderNumber>12345678</orderNumber>
<customer>
<customerNumber>5678</customerNumber>
<firstName>John</firstName>
<lastName>Smith</lastName>
</customer>
<billTo state="WA" postCode="98059">
<street1>12345 Happy Lane</street1>
<city>Plunk</city>
<country>USA</country>
</billTo>
<shipping>PRIORITY_MAIL</shipping>
<shipTo state="WA" postCode="98034">
<street1>333 River Avenue</street1>
<city>Kirkland</city>
</shipTo>
<item quantity="1" price="5.99" id="FA9498349851"/>
<item quantity="2" price="9.50" id="GC1234905049"/>
<item quantity="1" price="8.95" id="AX9300048820"/>
</order> |
下载包还包括一个简单测试程序,它在本文中显示为清单 4,用于演示如何使用 JiBX 解组 及编组 文档。编组是在内存中生成对象的 XML 表示的过程,可能包括从初始对象链接的对象;解组是编组的反向过程,它将通过 XML 表示在内存中构建一个对象(还有可能是一些链接的对象)。Ant run 目标将执行此测试程序,使用 清单 3 文档作为输入并把编组后的文档副本写到名为 out.xml 的文件中。
清单 4. 测试程序
public class Test
{
/**
* Unmarshal the sample document from a file, compute and set order total, then
* marshal it back out to another file.
*
* @param args
*/
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java -cp ... " +
"org.jibx.starter.Test in-file out-file");
System.exit(0);
}
try {
// unmarshal customer information from file
IBindingFactory bfact = BindingDirectory.getFactory(Order.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
FileInputStream in = new FileInputStream(args[0]);
Order order = (Order)uctx.unmarshalDocument(in, null);
// compute the total amount of the order
float total = 0.0f;
for (Iterator<Item> iter = order.getItems().iterator(); iter.hasNext();) {
Item item = iter.next();
total += item.getPrice() * item.getQuantity();
}
order.setTotal(new Float(total));
// marshal object back out to file (with nice indentation, as UTF-8)
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
FileOutputStream out = new FileOutputStream(args[1]);
mctx.setOutput(out, null);
mctx.marshalDocument(order);
System.out.println("Processed order with " + order.getItems().size() +
" items and total value " + total);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (JiBXException e) {
e.printStackTrace();
System.exit(1);
}
}
} |
图 3 显示了运行 run 目标时应当会看到的输出:
图 3. Ant 构建 run 任务
这是 第 1 部分 中使用的同一个测试程序,并且同样具有第一部分教程中讨论的限制。就像在第 1 部分中一样,out.xml 文件包含了将解组原始文档获得的订单数据重新编组后生成的输出。
|  |
|