简体   繁体   中英

Remove xsd element when converting xsd to java using jaxb

I'm going to generate java code from the xsd. I want to know how to remove xsd element when converting xsd to java using jaxb. My goal is to ignore message

Ex:

<xs:element name="note">
      <xs:complexType>
        <xs:sequence>
          <xs:element type="xs:string" name="name"/>
          <xs:element type="xs:string" name="message"/>
        </xs:sequence>
      </xs:complexType>
</xs:element>

Java Variables

        @XmlElement
        protected String name;

In here you can see that message element got removed. I want to know how to do that?

Can you alter the .xsd ? The current Element "note" tells that both "name" and "message" are mandatory. If you want "message" to be conditional you probably need to add minOccurs="0" attribute:

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element type="xs:string" name="name"/>
      <xs:element type="xs:string" name="message" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

If I understand you correctly, to sum it up you want to ignore a mandatory element and still want the result to validate correctly. My short answer is that this is contradictory and does not make sense. JAXB is built to do it correctly and you want do it wrongly(?).

You have several options, eg.

  • Live with the extra constructor argument.
  • Generate no-arg constructor instead.
  • Don't generate, but hand code JAXB annotated classes. Maybe turn off some validation.
  • Don't use JAXB, use something else (dom4j et. al.) or handcraft the xml
  • Possibly you can add some overriding in a binding file or even a plug-in, but I wouldn't think it was worth it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM