JAXB xjc 映射到现有域对象 [英] JAXB xjc mapping to existing domain objects

查看:40
本文介绍了JAXB xjc 映射到现有域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了大量搜索,但找不到一个简洁的示例,说明如何将 XML 模式映射到现有域对象,而不是使用 xjc 创建全新的域对象.我已经创建了一个绑定 (xjb) 文件,但仍然找不到完成此操作的方法.

I have done a lot of searching and cannot find a concise example of how to map an XML schema to existing domain objects instead of creating brand new ones utilizing xjc. I have created a bindings (xjb) file but still can find no way of accomplishing this.

如果我有一个现有的域对象,我希望 JAXB 使用如下:

If I have an existing domain Object that I want JAXB to use such as the following:

package com.blah.domain;
class CustomerOffice{
   private int id;
   private String name;
   private String phone;
}

我有一个如下所示的 XML 架构:

And I have an XML Schema like the following:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:www="http://www.blah.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.blah.com" elementFormDefault="unqualified">
   <xs:element name="Customer">
      <xs:complexType>
         <xs:sequence>
           <xs:element name="id" type="xs:int" nillable="false" minOccurs="1" maxOccurs="1"/>
           <xs:element name="name" type="xs:string"/>
           <xs:element name="city" type="xs:string"/>
           <xs:element name="CustomerOffice" type="www:CustomerOffice" maxOccurs="unbounded"/>
        </xs:sequence>
     </xs:complexType>
   </xs:element>
   <xs:complexType name="CustomerOffice">
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="length" type="xs:int"/>
      </xs:sequence>
   </xs:complexType>
</xs:schema>

如果我使用 xjc 生成 JAXB 类,它将创建一个名为 Customer(我想要的)的新类.它还将创建一个名为 CustomerOffice 的新类(我不想要它,我希望它使用我现有的域对象).

If I go to generate the JAXB classes with xjc it will create a new class called Customer (which I want). It will also create a new class called CustomerOffice (which I don't want, I want it to use my existing domain object).

因此,我希望它使用现有的 com.blah.domain.CustomerOffice,而不是指向type:www:CustomerOffice"的架构.

So instead of the schema pointing to "type:www:CustomerOffice" I would want it to use the existing com.blah.domain.CustomerOffice.

我试图使这个尽可能简单的例子,任何帮助表示赞赏.

I tried to make this as simple an example as possible, any help is appreciated.

推荐答案

您可以使用外部绑定文件来配置 XJC 来做您想做的事.

You can use an external binding file to configure XJC to do what you want.

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="yourSchema.xsd">
        <jxb:bindings node="//xs:complexType[@name='CustomerOffice']">
            <jxb:class ref="com.blah.domain.CustomerOffice"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

XJC 电话

xjc -d outputDir -b binding.xml yourSchema.xsd

这篇关于JAXB xjc 映射到现有域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆