如何在jaxb xjc中重命名嵌套的类 [英] how to rename nested classes in jaxb xjc

查看:83
本文介绍了如何在jaxb xjc中重命名嵌套的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导入架构的wsdl 我正在尝试解决xjc用相同的名称命名多个嵌套的MyElementName类的问题-无法编译 我在下面创建了此绑定文件,但显示错误:

I have a wsdl that imports a schema I am trying to resolve xjc naming multiple nested MyElementName classes with the same name - which doesn't compile I have created this binding file below but it gives the error:

parsing a schema...
compiling a schema...
Exception in thread "main" java.lang.IllegalArgumentException: Illegal class inheritance loop.  
Outer class MyElementName1 may not subclass from inner class: MyElementName1
    at com.sun.codemodel.internal.JDefinedClass._extends(JDefinedClass.java:257)
    at com.sun.tools.internal.xjc.generator.bean.ImplStructureStrategy$1._extends(ImplStructureStrategy.java:104)
    at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.<init>(BeanGenerator.java:197)
    at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:151)
    at com.sun.tools.internal.xjc.model.Model.generateCode(Model.java:275)
    at com.sun.tools.internal.xjc.Driver.run(Driver.java:342)
    at com.sun.tools.internal.xjc.Driver.run(Driver.java:184)
    at com.sun.tools.internal.xjc.Driver._main(Driver.java:107)
    at com.sun.tools.internal.xjc.Driver.access$000(Driver.java:64)
    at com.sun.tools.internal.xjc.Driver$1.run(Driver.java:87)

我删除了MyElementName1的第一个绑定,它可以正常工作,但仅命名类中的1个,我想更改两个名称, 我如何更改绑定文件,以便它将两个旧的嵌套类正确命名为我在下面选择的两个唯一名称.我之所以要这样做,是因为我还有其他四个名称相同的嵌套类的映射,因此需要命名每个类来解决更多嵌套的问题区域

I I remove the first binding for MyElementName1 it works correctly but only names 1 of the classes, I want to change both names, how do I change the binding file so that it will properly name both old nested classes to two unique names I have picked below. I want to do this because I have other mappings with 4 nested classes with the same name so being able to name each class would be needed to tackle the more nested problem areas

<jaxb:bindings 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:bindings schemaLocation="MYWSDL.wsdl">    
        <jaxb:bindings node="//xsd:element[@name='AAAA']//xsd:complexType//xsd:sequence/xsd:element[@name='BBB']//xsd:complexType/xsd:sequence//xsd:element[@name='MyElementName']">
            <jaxb:class name="MyElementName1"/>
        </jaxb:bindings>               
        <jaxb:bindings node="//xsd:element[@name='AAAA']//xsd:sequence/xsd:element[@name='BBB']//xsd:element[@name='MyElementName']//xsd:sequence/xsd:element[@name='CCC']//xsd:sequence/xsd:element[@name='MyElementName']/xsd:complexType">
            <jaxb:class name="MyElementName2"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

这是xsd(字段名称已更改,但保持不变)

here is xsd (names of fields changed but rest same)

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element minOccurs="0" name="AAA">
<xsd:complexType> 
    <xsd:element minOccurs="0" name="BBB">
     <xsd:complexType> 
          <xsd:element name="MyElementName">        
             <xsd:complexType>              
              <xsd:sequence>
                <xsd:element minOccurs="0" name="CCC">
                  <xsd:complexType>
                    <xsd:sequence>
                      <xsd:element name="MyElementName">
                        <xsd:complexType>
                          <xsd:sequence>
                            <xsd:element ref="FromDate" />
                            <xsd:element ref="ThruDate" />
                          </xsd:sequence>
                        </xsd:complexType>
                      </xsd:element>
                      <xsd:element name="DDD">
                        <xsd:complexType>
                          <xsd:sequence>
                            <xsd:element ref="DueDate" />
                          </xsd:sequence>
                        </xsd:complexType>
                      </xsd:element>
                    </xsd:sequence>
                  </xsd:complexType>
                </xsd:element>
                <xsd:element minOccurs="0" ref="YYYY" />
                <xsd:element minOccurs="0" ref="ZZZZ" />
                </xsd:element>
              </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:complexType>             
    </xsd:element>               
</xsd:complexType>             
</xsd:element>  
</xsd:schema>     

字段MyElementName2可以,如果我在下面尝试绑定,该字段将内置到java文件中,它只是jaxb映射到多个子项的第一个-包括与MyElementName2映射的子项. XPath总是映射到一个节点,因此它可能是一个jaxb选项,因为它似乎正在扩展输入

the field MyElementName2 is ok, that gets built into the java file if I try binding below, its just the first one that jaxb is mapping to the multiple children - including the one that is mapped with MyElementName2. XPath always maps to one node so maybe its a jaxb option, as it seems to be expanding the input

这是在下面导致错误的替代绑定

here is the alternative binding that results in its error below

<jaxb:bindings 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:bindings schemaLocation="MYWSDL.wsdl">    
        <jaxb:bindings node="//xsd:element[@name='AAA']//xsd:sequence/xsd:element[@name='BBB']//xsd:element[@name='MyElementName']">
            <jaxb:class name="MyElementName1"/>
        </jaxb:bindings>        
        <jaxb:bindings node="//xsd:element[@name='AAA']//xsd:sequence/xsd:element[@name='BBB']//xsd:element[@name='Billing']//xsd:sequence/xsd:element[@name='CCC']//xsd:sequence/xsd:element[@name='MyElementName']/xsd:complexType">
            <jaxb:class name="MyElementName2"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

错误:

parsing a schema...
[ERROR] XPath evaluation of "//xsd:element[@name='AAA']//xsd:sequence/xsd:element[@name='BBB']//xsd:element[@name='MyElementName']" results in too many target nodes
Failed to parse a schema.

推荐答案

JAXB会为一种复杂类型生成一个类,因此我认为您应该将自定义类名称绑定到xs:complexType而不是xs:element.您的第一个XPath指向xs:element,我认为JAXB不知道如何将一个类绑定到它.试试这个绑定:

JAXB generates one class per one complex type, so I believe you should bind your custom class name to xs:complexType rather than xs:element. Your first XPath refers to the xs:element and I think JAXB does not know how to bind a class to it. Try this binding:

<jaxb:bindings 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:bindings schemaLocation="MYWSDL.wsdl">    
    <jaxb:bindings node="//xsd:element[@name='AAAA']//xsd:complexType//xsd:sequence/xsd:element[@name='BBB']//xsd:complexType/xsd:sequence//xsd:element[@name='MyElementName']/xs:complexType">
        <jaxb:class name="MyElementName1"/>
    </jaxb:bindings>               
    <jaxb:bindings node="//xsd:element[@name='AAAA']//xsd:sequence/xsd:element[@name='BBB']//xsd:element[@name='MyElementName']//xsd:sequence/xsd:element[@name='CCC']//xsd:sequence/xsd:element[@name='MyElementName']/xsd:complexType">
        <jaxb:class name="MyElementName2"/>
    </jaxb:bindings>
</jaxb:bindings>

就个人而言,我更喜欢使用JAXB而不是XMLBeans.您应谨慎使用后者,因为在编译深度嵌套的架构时,由于文件名长度限制(通常为255个字符),它可能会生成无法编译的嵌套类结构.

Personally, I prefer using JAXB to XMLBeans. You should be careful with the latter, because when compiling a deeply nested schema it may generate a structure of nested classes impossible to compile due to filename length limitations (usually 255 characters).

这篇关于如何在jaxb xjc中重命名嵌套的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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