Генерация классов из файла XSD - PullRequest
0 голосов
/ 21 ноября 2018

Я пытаюсь сгенерировать классы Java из spring-beans.xsd с помощью JAXB (только для учебных целей), и у меня появляется эта ошибка:

parsing a schema...
[ERROR] Property "Ref" is already defined. Use <jaxb:property> to resolve this conflict.
  line 582 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 622 of file:/C:/spring-beans.xsd

[ERROR] Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
  line 584 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 629 of file:/C:/spring-beans.xsd

[ERROR] Property "Key" is already defined. Use <jaxb:property> to resolve this conflict.
  line 1135 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 1138 of file:/C:/spring-beans.xsd

[ERROR] Property "Ref" is already defined. Use <jaxb:property> to resolve this conflict.
  line 1052 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 1071 of file:/C:/spring-beans.xsd

[ERROR] Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
  line 1054 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 1078 of file:/C:/spring-beans.xsd

Failed to parse a schema.

Что не так?Спасибо за любую помощь заранее.

1 Ответ

0 голосов
/ 24 ноября 2018

Проблема в том, что в каждом случае у вас есть два элемента XML - элемент типа

<xsd:element ref="ref"/>

и атрибут типа

<xsd:attribute name="ref" type="xsd:string"/>

в одном сложном типе.По умолчанию они оба сопоставляются со свойствами с именем Ref.Но поскольку эти два XML-элемента - разные вещи, JAXB жалуется на конфликт имен.

Решение, как правило, довольно простое (и фактически предлагается в журнале ошибок).Настройте одно из свойств, используя привязку jaxb:property.Нечто подобное:

<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jaxb:bindings 
        schemaLocation="spring-beans.xsd" 
        node="/xs:schema">
        <jaxb:bindings node="xs:element[@name='constructor-arg']/xs:complexType/xs:attribute[@name='ref']">
            <jaxb:property name="aRef"/>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>
...