we using jaxb generate java classes , have encountered few cases generated plural method names not correct. example, expect getphysicians
getting getphysicien
. how customize how jaxb pluralizes specific methods?
the schema:
<xs:complextype name="physician"> <xs:sequence> ... </xs:sequence> </xs:complextype> <xs:complextype name="physicianlist"> <xs:sequence> <xs:element name="physician" type="physician" minoccurs="0" maxoccurs="unbounded"/> </xs:sequence> </xs:complextype>
the generated java code:
... public class physicianlist { ... @xmlelement(name = "physician") protected list<physician> physicien; ... public list<physician> getphysicien() { if (physicien == null) { physicien = new arraylist<physician>(); } return this.physicien; }
update
this has been answered blaise. however, prefer not mixing concerns such jaxb customizations in xml schema. of same preference, here jaxb binding file achieves same thing blaise suggested, keeping jaxb customization out of schema:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/xmlschema" version="2.0"> <jaxb:bindings schemalocation="myschema.xsd"> <jaxb:bindings node="//xs:complextype[@name='physicianlist']//xs:element[@name='physician']"> <jaxb:property name="physicians"/> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings>
by default following generated schema fragment:
import java.util.arraylist; import java.util.list; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmltype; @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "physicianlist", proporder = { "physician" }) public class physicianlist { @xmlelement(name = "physician") protected list<physician> physician; public list<physician> getphysician() { if (physician == null) { physician = new arraylist<physician>(); } return this.physician; } }
if annotate xml schema:
<xs:schema xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/xmlschema" jaxb:version="2.1"> <xs:complextype name="physician"> <xs:sequence> </xs:sequence> </xs:complextype> <xs:complextype name="physicianlist"> <xs:sequence> <xs:element name="physician" type="physician" minoccurs="0" maxoccurs="unbounded"> <xs:annotation> <xs:appinfo> <jaxb:property name="physicians"/> </xs:appinfo> </xs:annotation> </xs:element> </xs:sequence> </xs:complextype> </xs:schema>
then can generate desired class:
import java.util.arraylist; import java.util.list; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmltype; @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "physicianlist", proporder = { "physicians" }) public class physicianlist { @xmlelement(name = "physician") protected list<physician> physicians; public list<physician> getphysicians() { if (physicians == null) { physicians = new arraylist<physician>(); } return this.physicians; } }
Comments
Post a Comment