如何使两个XSD使用相同的自定义对象 [英] How to make two XSDs to use the same custom object

查看:75
本文介绍了如何使两个XSD使用相同的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB和intellij webservices插件从XSD创建java文件。我有两个定义相同对象的XSD,但是当我使用从XML模式生成java代码创建它们时,该对象使用自己的包创建两次。我已经尝试使用import xsd并使用ref属性,我得到了相同的结果。

I'm using JAXB and intellij webservices plugin to create java files from XSDs. I have two XSDs that defining the same object but when I create them using the "generate java code from XML schema" the object is created twice with his own package. I already tried with import xsd and using the ref attribute and I get the same result.

这是一个例子:

这是第一个XSD

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema targetNamespace="http://www.msp-gs.com/workflow"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
       xmlns:wc="http://www.example.com/workflow"
       attributeFormDefault="unqualified"
       elementFormDefault="qualified"
       jaxb:version="1.0">

    <xs:annotation>
    <xs:appinfo>
        <jaxb:globalBindings enableJavaNamingConventions="true">

        </jaxb:globalBindings>
    </xs:appinfo>
    </xs:annotation>

    <xs:element name="WC">

    <xs:complexType>

        <xs:sequence>

            <xs:element name="Example"
                        type="wc:Restriction"
                        minOccurs="1"
                        maxOccurs="unbounded">

            </xs:element>

        </xs:sequence>

        </xs:complexType>

    </xs:element>

    <xs:complexType name="Restriction">

        <xs:attribute type="xs:string"
                  name="authorizationTreeId"/>

    </xs:complexType>

    </xs:schema>

这是第二个XSD

    <?xml version="1.0" encoding="UTF-8"?>
     <xs:schema targetNamespace="http://www.msp-gs.com/workflow"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
       xmlns:fd="http://www.example.com/workflow"
       attributeFormDefault="unqualified"
       elementFormDefault="qualified"
       jaxb:version="1.0">

      <xs:annotation>
        <xs:appinfo>
        <jaxb:globalBindings enableJavaNamingConventions="true">

        </jaxb:globalBindings>
    </xs:appinfo>
      </xs:annotation>

    <xs:element name="FD">

        <xs:complexType>

        <xs:sequence>

            <xs:element name="Example"
                        type="fd:Restriction"
                        minOccurs="1"
                        maxOccurs="unbounded">

            </xs:element>

        </xs:sequence>

    </xs:complexType>

    </xs:element>

    <xs:complexType name="Restriction">

    <xs:attribute type="xs:string"
                  name="authorizationTreeId"/>

    </xs:complexType>

     </xs:schema>

我希望限制将是同一个对象。

I want that Restriction will be the same object.

谢谢。

推荐答案

您可以告诉JAXB使用现有Java类而不是使用外部绑定生成Java类文件如下。在下面的示例中,我们告诉JAXB使用现有的 Product 类:

You can tell JAXB to use an existing Java class instead of generating one using an external binding file like the following. In the example below we are telling JAXB to use the existing Product class:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
  <bindings scd="x-schema::tns"
    xmlns:tns="http://www.example.org/Product">
    <schemaBindings map="false"/>
    <bindings scd="tns:product">
      <class ref="org.example.product.Product"/>
    </bindings>
  </bindings>
</bindings>

如果您使用XJC工具从XML模式生成类,则可以使用 -episode 标志让XJC生成一个指向它生成的所有类的绑定文件。这将允许您重复使用以前生成的类。

If you are using the XJC tool to generate classes from an XML schema, you can use the -episode flag to have XJC generate a binding file pointing to all the classes that it generated. This will allow you to reuse previously generated classes.

xjc -d out -episode product.episode Product.xsd

更多信息

  • http://blog.bdoughan.com/2011/12/reusing-generated-jaxb-classes.html

这篇关于如何使两个XSD使用相同的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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