2

I need to force the xsi:type generation on fields because the xml will be deserialized into a different object through a xsl transformation sheet. Datatypes are defined with XSD schemas, anyone can use jaxb to generate classes and send data to the endpoint but the endpoint is completely dynamic and uses a special DTO containing common data fields and some Object maps which will hold the dynamic data fields.

For example this is what I get:

<?xml version="1.0" encoding="UTF-8"?>
<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <myp:sys_name>name</myp:sys_name>
  <myp:sys_path>path</myp:sys_path>
  <myp:sys_type>type</myp:sys_type>
  <myp:dyn_date_modified>2015-09-30T11:13:10.810+02:00</myp:dyn_date_modified>
</myp:documento>

this is what I need:

<?xml version="1.0" encoding="UTF-8"?>
<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <myp:sys_name>name</myp:sys_name>
  <myp:sys_path>path</myp:sys_path>
  <myp:sys_type>type</myp:sys_type>
  <myp:dyn_date_modified xsi:type="xs:dateTime">2015-09-30T11:13:10.810+02:00</myp:dyn_date_modified>
</myp:documento>

because the attribute dyn_date_modified will be deserialized into the dynamic Object map..

Let me know if you need more detail.

ntrp
  • 402
  • 5
  • 12
  • Why `xs:string` should be needed is beyond me: basically all of XML is as string as can be. -- But you won't get this from JAXB anyway. – laune Sep 29 '15 at 17:11
  • It was needed because the deserializing object contains Object maps so if the type was xs:dateTime it would know ho to deserialize.. Anyway I "resolved" by applying XSL preprocessing on the received xml because seems that with jaxb it is not possible as you stated. I will update my question with a different type cause xs:string is not clear.. – ntrp Sep 30 '15 at 09:25

1 Answers1

0

After another day of research seems that JAXB doesn't offer this functionality because it is supposed to marshal and unmarshal xml using always the same object. In my case I need to marshal xml from a Class generated by xjc through a XSD and then unmarshal with an annotated DTO which contains a number of fixed fields and three maps for dynamic fields (starting with def_, dyn_ and mul_). I resolved my requirement by adding a xsl preprocessing stage which maps prefixed fields to maps and appends xsi:type based on name prefix.

<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <myp:sys_uuid>2ca56a7e9ca62dcd2d2ad8eeca100fd24</myp:sys_uuid>
  <myp:sys_name>2127a09acda7bf6acf26257bb80372f90</myp:sys_name>
  <myp:dyn_date_modified>2015-09-30T11:13:10.810+02:00</myp:dyn_date_modified>
</myp:documento>

converts to:

<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <myp:sys_uuid>uuid</myp:sys_uuid>
  <myp:sys_name>name</myp:sys_name>
  <myp:dynFields>
    <entry>
        <key>dyn_date_modified</key>
        <value xsi:type="xs:dateTime">2015-09-30T11:13:10.810+02:00</value>
    </entry>
  </myp:dynFields>
</myp:documento>

and then gets correctly unmarshalled.

ntrp
  • 402
  • 5
  • 12