使用MOXy生成具有默认minOccurs的模式

时间:2010-12-16 11:15:08

标签: java xml jaxb eclipselink moxy

我正在尝试使用EclipseLink MOXy 2.1.2和Java 1.6来生成基于我的域类的模式,遵循以下示例:http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema

但是默认的minOccurs值存在问题。如果注释@XmlElement未指定必需属性,则它应默认为false,这意味着在生成模式时,minOccurs应为0.这与Sun的JAXB实现一样正常。但是,当使用Moxy时,不会生成minOccurs,并隐含默认值1。

例如,

public Integer getDuration() {
   return duration;
}

应该已生成

<xs:element name="duration" type="xsd:int" minOccurs="0"/>

相反,如果使用Moxy则生成

<xsd:element name="duration" type="xsd:int"/>

表示minOccurs =“1”,因为那是默认值。这是一个错误吗?有没有解决方法呢?

提前致谢!

2 个答案:

答案 0 :(得分:1)

您是否有机会使用EclipseLink 2.1.2的预发布版本? EclipseLink 2.1.2的发布版本可以在这里获得:

当我运行以下示例时:

package minoccurs;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Demo {

    public static class Root {

        private Integer duration; 

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

我明白了:

org.eclipse.persistence.jaxb.JAXBContext@16cd7d5
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

更新#1

根据您的update,这是一个问题。我打开了以下EclipseLink MOXy错误:

更新#2

我们已经在EclipseLink 2.2流中解决了这个错误。修复可以从12月21日开始的夜间下载中获得:

此修复程序最终也将包含在2.1.3补丁中。

答案 1 :(得分:0)

感谢您的回复,Blaise。这个问题似乎是因为我试图通过指定propOrder = {}来生成xsd:all。使用该注释,MOXy不会生成minOccurs =“0”。我已经修改了你的演示类来演示这个问题:

package moxy.test;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlType;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Test {

    @XmlType(propOrder = {})
    public static class Root {

        private Integer duration;
        private Integer period;

        public Integer getPeriod() {
            return this.period;
        }

        public void setPeriod(Integer period) {
            this.period = period;
        }

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

使用MOXy生成以下内容。请注意,没有minOccurs属性:

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:all>
         <xsd:element name="duration" type="xsd:int"/>
         <xsd:element name="period" type="xsd:int"/>
      </xsd:all>
   </xsd:complexType>
</xsd:schema>

如果删除@XmlType(propOrder = {})注释,生成的模式将存在minOccurs,但元素为序列:

org.eclipse.persistence.jaxb.JAXBContext@cdedfd
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
         <xsd:element name="period" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

仅供参考,以下是Sun的JAXB参考实现生成的内容:

jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl.class Build-Id: 1.6.0_21
...

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="root">
    <xs:all>
      <xs:element name="duration" type="xs:int" minOccurs="0"/>
      <xs:element name="period" type="xs:int" minOccurs="0"/>
    </xs:all>
  </xs:complexType>
</xs:schema>

提前致谢!