How to override the default name for in JAXB using a external binding file?

孤街醉人 提交于 2019-12-22 08:54:44

问题


I have an element that looks something like this.

 <xsd:element name="container">
    <xsd:complexType>
      <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element ref="navmap"/>
        <xsd:element ref="keymap" />
        <xsd:element ref="container" />
        <xsd:element ref="ad" />
        <xsd:element ref="button" />
        <xsd:element ref="checkbox" />        
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>

Here is the default code that gets created for this element.

@XmlElements({
    @XmlElement(name = "navmap", type = Navmap.class),
    @XmlElement(name = "keymap", type = Keymap.class),
    @XmlElement(name = "container", type = Container.class),
    @XmlElement(name = "ad", type = Ad.class),
    @XmlElement(name = "button", type = Button.class),
    @XmlElement(name = "checkbox", type = Checkbox.class),
})
protected List<Object> navmapOrKeymapOrContainer;

My question is What do I need to put in my .xjb bindings file to change the default generated name from navmapOrKeymapOrContainer to something else like children?


回答1:


Example:

<xs:complexType>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:annotation>
      <xs:appinfo>
        <jaxb:property name="Shapes"/>
      </xs:appinfo>
    </xs:annotation>
    <xs:element name="Rectangle" type="Rectangle"/>
    <xs:element name="Square" type="Square"/>
    <xs:element name="Circle" type="Circle"/>
  </xs:choice>
</xs:complexType>

Adapt this in your binding file and it will do. See here for reference.

Listing 11 tells the secret:

<jxb:bindings version="1.0" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
  jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="po4.xsd" node="/xs:schema">
  <jxb:globalBindings>
    <xjc:superClass name="com.syh.
    <xjc:serializable uid="12343"/>
  </jxb:globalBindings>
  <jxb:bindings node="//xs:element[@name='Widgets']//xs:complexType//xs:choice">
      <jxb:property name="Shapes"/>
  </jxb:bindings>
</jxb:bindings>


来源:https://stackoverflow.com/questions/6957568/how-to-override-the-default-name-for-in-jaxb-using-a-external-binding-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!