代码之家  ›  专栏  ›  技术社区  ›  Matthias Kegler

操纵JAXB来创建IDREF元素,而不是JaxBElement<?>

  •  0
  • Matthias Kegler  · 技术社区  · 6 年前

    我想使用maven-jaxb2-plugin生成类。

    我的输入xsd如下所示:

    <complexType>
    <complexContent>
        <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
            <sequence>
                <element name="ReferencedElementA" type="{http://www.w3.org/2001/XMLSchema}IDREFS" minOccurs="0" />
                <element name="ReferencedElementB" type="{http://www.w3.org/2001/XMLSchema}IDREF"
                    maxOccurs="unbounded" minOccurs="0" />
            </sequence>
        </restriction>
    </complexContent>
    

    请注意,我不能更改模式,因为它是由外部客户提供的!

    这是生成的代码:

    @XmlList
    @XmlElement(name = "ReferencedElementA")
    @XmlIDREF
    @XmlSchemaType(name = "IDREFS")
    protected List<Object> referencedElementA;
    @XmlElementRef(name = "ReferencedElementB", namespace = "ABC", type = JAXBElement.class, required = false)
    protected List<JAXBElement<Object>> referencedElementB;
    

    如您所见,“ReferenceElementB”作为列表生成>使用@XMLElementRef注释。

    我想要的是如下所示:

    @XmlElement(name = "ReferencedElementB")
    @XmlIDREF
    @XmlSchemaType(name = "IDREF")
    protected List<Object> referencedElementB;
    

    使用来自的解决方法 Blaise Doughan ,我用以下代码扩展了绑定文件:

    <bindings node = ".//xs:element[@name='ReferencedElementB']">
        <class ref="java.lang.Object"/>
    </bindings>
    

    这几乎完成了任务,生成了如下代码:

    @XmlElement(name = "ReferencedElementB")
    @XmlSchemaType(name = "IDREF")
    protected List<Object> referencedElementB;
    

    但是,仍然缺少@XmlIDREF注释。这会在编组过程中产生错误。

    我试图通过annox插件注入缺失的注释:

    <bindings node = ".//xs:element[@name='ReferencedElementB']">
        <class ref="java.lang.Object"/>
        <annox:annotate target="field">@javax.xml.bind.annotation.XmlIDREF</annox:annotate>
    </bindings>
    

    但这一代人失败了

    compiler was unable to honor this annox:annotate customization. It is attached to a wrong place, or its inconsistent with other bindings
    

    再一次,我无法更改输入xsd。

    提前感谢您!

    1 回复  |  直到 6 年前
        1
  •  0
  •   lexicore    6 年前

    请注意,我不能更改模式,因为它是由外部客户提供的!

    我一直在这里读这篇文章,所以我总是想知道——为什么?为什么不能更改模式?

    当然 您可以在编译之前更改模式。获取原始模式并应用补丁/XSLT/任何东西来修改它。只要结果在结构上与原始模式匹配,就可以了。

    考虑一下您的情况:您使用技巧和变通方法来有效地破解生成的代码。与编译之前修改模式相比,它有多好?我不认为这更好。它更复杂,更容易出错,最后不能说生成的代码是模式的反映。实际上,您的代码没有模式。你甚至不能说你的黑客生成的代码是否充分反映了原始模式。

    在编译之前修改模式非常容易。您可以轻松地比较原始的和修改的,并查看更改是否兼容。生成很简单,不需要黑客和变通方法。


    现在谈谈你的问题。

    绑定

    <annox:annotate target="field">@javax.xml.bind.annotation.XmlIDREF</annox:annotate>
    

    对我来说很好。由于某些原因,在模式编译期间不考虑它,这就是为什么会出现错误的原因。

    很难说为什么不考虑。我会尝试以下方法:

    • 检查编译中是否实际包含/打开JAXB2基本注释插件:

          <plugin>
              <groupId>org.jvnet.jaxb2.maven2</groupId>
              <artifactId>maven-jaxb2-plugin</artifactId>
              <configuration>
                  <extension>true</extension>
                  <args>
                      <arg>-Xannotate</arg>
                  </args>
                  <plugins>
                      <plugin>
                          <groupId>org.jvnet.jaxb2_commons</groupId>
                          <artifactId>jaxb2-basics-annotate</artifactId>
                      </plugin>
                  </plugins>
              </configuration>
          </plugin>
      
    • 将自定义元素放在自己的 bindings 要素也许它与 class 出于某种原因。

    我现在看不出还有什么办法。

    如果没有帮助,请在此处向我发送带有最小复制项目的PR:

    https://github.com/highsource/jaxb2-annotate-plugin-support

    在i/idrefs下。我来看看。

    免责声明: 我是 ,则,

    推荐文章