java - How do I specify the adapter(s) which JAXB uses for marshaling/unmarshaling data? -


is there way specify adapter jaxb uses marshaling/unmarshaling objects in xml schema?

for example, if want parse following integer:

<specialvalue>0x1234</specialvalue> 

i can use following in schema:

<xs:simpletype name="hexint">     <xs:annotation>          <xs:appinfo>             <jaxb:javatype name="int" parsemethod="integer.decode" />         </xs:appinfo>     </xs:annotation>       <xs:restriction base="xs:string">         <xs:pattern value="0x[0-9a-fa-f]+" />     </xs:restriction> </xs:simpletype> <xs:element name="specialvalue" type="hexint" /> 

when run schema through xjc tool, string "0x1234" should decoded using integer.decode() integer value 0x1234, or 4660 in decimal. adapter class generated reflects properly:

public integer unmarshal(string value) {     return (integer.decode(value)); } 

however, when want marshal value xml element (which string literal), adapter class following:

public string marshal(integer value) {     if (value == null) {         return null;     }     return value.tostring(); } 

in case, string value of integer 0x1234 (4660 in decimal) "4660", not adhere schema (because has no "0x" prefix).

how can tell adapter want integer 0x1234 marshalled "0x1234" string literal? able within schema can generate new java classes without having modify output. possible?

edit: based on accepted answer, here did working:

i wrote method in class com.example.parse called tohexstring():

public static string tohexstring(int value) {     return ("0x" + integer.tohexstring(value)); } 

then pointed schema method printing:

<xs:simpletype name="hexint">     <xs:annotation>          <xs:appinfo>             <jaxb:javatype name="int" parsemethod="integer.decode" printmethod="com.example.parse.tohexstring" />         </xs:appinfo>     </xs:annotation>       <xs:restriction base="xs:string">         <xs:pattern value="0x[0-9a-fa-f]+" />     </xs:restriction> </xs:simpletype> 

try this

<jaxb:javatype name="int" parsemethod="integer.decode"                            printmethod="integer.tohexstring"/> 

i haven't tested though remember using similar.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -