JAX-B - 如何将架构元素映射到现有Java类 [英] JAX-B - How to map a schema element to an existing Java class

查看:82
本文介绍了JAX-B - 如何将架构元素映射到现有Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jaxb xjc映射到现有域对象

我正在使用JAX-B从XML模式生成Java类。

I am using JAX-B to generate Java classes from an XML schema.

我希望将模式中的一个元素绑定到项目中存在的Java类。我的绑定是在.xjb文件中完成的。我已经尝试了几种方法,但无法获得任何工作。

There is one element in my schema that I would like to bind to a Java class that exists in my project. My binding is done in a .xjb file. I have tried several approaches but can't get anything to work.

这可能吗?
如果是这样,怎么样?

Is this possible? If so, how?

这是我的问题的一个较小的例子:

Here is a smaller example of my problem:

My Existing Java class:

My Existing Java class:

package com.existing; 

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
public class Existing {
    private String prop; 
    public String getProp() { return prop; }
    public void setProp(String prop) { this.prop = prop; }
}

我的架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     targetNamespace="http://www.baloiselife.com/xpression/policy"
     xmlns="http://www.baloiselife.com/xpression/policy" >

<xs:element name="root_node">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="some_other_propery" type="xs:string"/>
      <!-- I want this element to map onto my existing Java class -->
      <xs:element name="special_element" type="existing_type" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

<!-- I want this element to be ignored, and instead my Java class used -->
<xs:complexType name="existing_type">
  <xs:sequence>
    <xs:element name="prop" type="xs:string" minOccurs="0" />
  </xs:sequence>
</xs:complexType>

那么任何想法我的绑定应该是什么?
我尝试使用jxb:class设置,但无法使其工作。
我的最终结果有两个要求:

So any ideas what my binding should be? I tried using the jxb:class setting, but could not get it to work. My end result has two requirements:


  1. ExistingType类是从生成的

  2. RootNode类有一个Existing类型的元素,它映射到我现有的Java类


推荐答案

您可以使用外部绑定文件来配置XJC以执行您想要的操作。

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

binding.xjb

<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='existing_type']">
            <jxb:class ref="com.existing.Existing"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

XJC致电

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

这篇关于JAX-B - 如何将架构元素映射到现有Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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