java - Issue with JAXB XMLAdapter marshalling -
i have requirement generate xml
file below format using jaxb2
, has both fixed , variable xml content.
what constraint?
the content of variable xml
part should 1 of 5 different xml schema
( planned have jaxb2.0
implemented 5 different java classes generate it) need embedded in fixed xml
content.
xml format:
<user_info> <header> //fixed xml part <msg_id>..</msg_id> <type>...</type> </header> <user_type> // variable xml content // (usertype : admin, reviewer, auditer, enduser, reporter) ........ </user_type> </user_info>
what tried ?
i have created jaxb
annotated java classes above xml metadata
. variable xml part, used common parent class (baseusertype
) extended 5 different classes <user_type>
. , tried override marshall(..)
operation using @xmljavatypeadapter
. (as below)
jaxb annotated class:
@xmlrootelement(name="user_info") public class userinfo { private header header; //reference jaxb annotated class header.class @xmljavatypeadapter(value=customxmladapter.class) private baseusertype usertype; // base class - acts common type // 5 different usertype jaxb annotated classes // getters setters here.. // tried declare jaxb annotations @ getter method }
custom xml adapter class:
public class customxmladapter extends xmladapter<writer, baseinfo> { private marshaller marshaller=null; @override public baseinfo unmarshal(writer v) throws exception { // implementations here... } @override public writer marshal(baseinfo v) throws exception { outputstream outstream = new bytearrayoutputstream(); writer strresult = new outputstreamwriter(outstream); if(v instanceof customerprofilerequest){ getmarshaller().marshal((customerprofilerequest)v, strresult ); } return strresult; } private marshaller getmarshaller() throws jaxbexception{ if(marshaller==null){ jaxbcontext jaxbcontext = jaxbcontext.newinstance(admin.class, reviewer.class, enduser.class, auditor.class, reporter.class); marshaller = jaxbcontext.createmarshaller(); } return marshaller; } }
where i'm struggling now?.
i'm not facing errors or warnings, xml
being generated (as shown below). output not expected one. doesn't embeds variable xml part fixed 1 correctly.
output
<user_info> <header> <msg_id>100</msg_id> <type>static</type> </header> <user_type/> // empty element, though binded value properly. </user_info>
my questions :
- why
jaxb marshallers
not embeds "customxmladapter
" marshalled content parent 1(userinfo.class)
. - do have any alternative option in
jaxb
simple? - how specify
boundtype
,valuetype
inxmladapter
. there specific type given in order embed content parent class marshalling?
an xmladapter
works allowing convert domain object, value object jaxb can better handle purposes of marshalling/unmarshalling.
if model objects other schemas sub classes of baseusertype
, need make jaxbcontext
aware of them. can when create jaxbcontext
having colon separated string package names.
jaxbcontext jc = jaxbcontext.newinstance("com.example.common:com.example.foo:com.example.bar");
Comments
Post a Comment