jaxb2 - JAXB XmlAdapter via bindings file -


consider xsd:

<xsd:schema targetnamespace="http://foobar" xmlns:tns="http://foobar"     xmlns:xsd="http://www.w3.org/2001/xmlschema">      <xsd:complextype name="idattribute">         <xsd:attribute name="id" type="xsd:token" />     </xsd:complextype>      <xsd:complextype name="foobartype">         <xsd:sequence>             <xsd:element name="someids" type="tns:idattribute" maxoccurs="unbounded" />         </xsd:sequence>     </xsd:complextype>      <xsd:element name="foobar" type="tns:foobartype" /> </xsd:schema> 

which results in following, generated java class:

@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "foobartype", proporder = {"someids"}) public class foobartype {      @xmlelement(required = true)     protected list<idattribute> someids;      // ... } 

because idattribute contains 1 string (id), want map these strings directly foobartype easier usage. therefore wrote xmladapter:

public class idattributeadapter extends xmladapter<idattribute, string> { ... } 

i've edited generated class manually verify xmladapter works expected (it does):

@xmlelement(required = true) @xmljavatypeadapter(idattributeadapter.class) protected list<string> someids; 

of course want have little code change done during code generation, i've added following bindings file (src/main/xjb/bindings.xjb):

attempt 1:

<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"     xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">      <jaxb:bindings schemalocation="../resources/xsd/foobar.xsd">         <jaxb:bindings node="//xsd:complextype[@name='idattribute']">             <xjc:javatype                 name="java.lang.string"                 adapter="org.foobar.idattributeadapter" />         </jaxb:bindings>     </jaxb:bindings>  </jaxb:bindings> 

result

com.sun.istack.saxparseexception2; systemid: file:/c:/dev/foobar/src/main/xjb/bindings.xjb; linenumber: 12; columnnumber: 91; compiler unable honor conversion customization. attached wrong place, or inconsistent other bindings. 

attempt 2 (from here):

<jaxb:globalbindings xmlns:foo="http://foobar" xsi:schemalocation="http://foobar ../resources/xsd/foobar.xsd">     <xjc:javatype         name="java.lang.string"         xmltype="foo:idattribute"         adapter="org.foobar.idattributeadapter" /> </jaxb:globalbindings> 

result

com.sun.istack.saxparseexception2; systemid: file:/c:/dev/projects/luna/oasch/src/main/xjb/bindings.xjb; linenumber: 8; columnnumber: 112; undefined simple type "{http://foobar}idattribute". 

i've tried few variations, resulted in similar errors. what's correct way add xmlapender using bindings file?

also see sample project on github.

what try wrong. should replace xml type idattribute java type string.

you have work generic dom.element type , convert <-> string. here exemple : jaxb @xmladapter arbitrary xml case replace java type bar string.

other solution add behavior generated class add methods foobartype work if has list of string
e.g. add methods
list<string> getids()
void setids(list<string>)

here link explain how can : "adding behavior"


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -