Rectangle 27 0

java How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file?


I did read some forums which mentioned writing an XML filter (I guess similar to what you've done) but I was hoping there was a more cleaner solution

I had similar problem. I was sending XML with these attributes to some WS that was not able to handle it. I remember that I was using Apache CXF for sending this XML so I ended up with CXF interceptor that handled removal of these attributes.

I would be happy if anyone gives you a better answer...

Sadly, I didn't find a way how to "disable" generation of these attributes directly in JAXB. What you can do (and probably will be the only way to solve it) is that you take your generated XML and process it once again with another (DOM/SAX) API and remove attributes manually. It is definitely not a nice solution but I'm not sure if you find better :-/

Note
Rectangle 27 0

java How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file?


@XmlElement(name = "DefaultCar", type=String.class) 
  protected Object defaultcar;

Thx Blaise, I read your solution to the other person who had a similar problem to me and thought I could use it too. However, it would be preferable not to alter the classes generated by JAXB but to code it in my program instead. I was hoping you pick up my question but it seems that EclipsLink MOXy doesn't provide any means to prevent this from happening then? :(

You can always override the property type using the type parameter on the @XmlElement annotation.

Note
Rectangle 27 0

java How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file?


@XmlElementRef
@XmlMixed
  • Make it String, why is it an Object in the first place?

@user3572079 You can't change the schema but you can customize, for instance using jaxb:javaType, see the update.

How you can fix that. You have align the type of the property with the type of the data. There's quite a number of ways to achieve that:

However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type.

JAXB exports xsi:type if your data specifies other type than your model. In your case, you set a string, but the field is Object. So your data has a different type than your model. The behaviour is correct.

Thx for that. I'll have a look at your update

thx for confirming my understanding of what's happening. Unfortunately, I can't change the type from Object to String as the XSD has been supplied to me (I did play about the method and changed it to accept String and the xsi:type goes way like you've said). Any good links to your second suggestion or does that involve changing the JAXB classes too? I'm using Eclipselink MOXy BTW. Is there some sort of feature that I can use? When I tried to cast the String to Object I was clutching at straws but with no luck! ;)

Note
Rectangle 27 0

java How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file?


@XmlElementRef
@XmlMixed
  • Make it String, why is it an Object in the first place?

@user3572079 You can't change the schema but you can customize, for instance using jaxb:javaType, see the update.

How you can fix that. You have align the type of the property with the type of the data. There's quite a number of ways to achieve that:

However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type.

JAXB exports xsi:type if your data specifies other type than your model. In your case, you set a string, but the field is Object. So your data has a different type than your model. The behaviour is correct.

Thx for that. I'll have a look at your update

thx for confirming my understanding of what's happening. Unfortunately, I can't change the type from Object to String as the XSD has been supplied to me (I did play about the method and changed it to accept String and the xsi:type goes way like you've said). Any good links to your second suggestion or does that involve changing the JAXB classes too? I'm using Eclipselink MOXy BTW. Is there some sort of feature that I can use? When I tried to cast the String to Object I was clutching at straws but with no luck! ;)

Note
Rectangle 27 0

java How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file?


@XmlElement(name = "DefaultCar", type=String.class) 
  protected Object defaultcar;

Thx Blaise, I read your solution to the other person who had a similar problem to me and thought I could use it too. However, it would be preferable not to alter the classes generated by JAXB but to code it in my program instead. I was hoping you pick up my question but it seems that EclipsLink MOXy doesn't provide any means to prevent this from happening then? :(

You can always override the property type using the type parameter on the @XmlElement annotation.

Note
Rectangle 27 0

java How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file?


I did read some forums which mentioned writing an XML filter (I guess similar to what you've done) but I was hoping there was a more cleaner solution

I had similar problem. I was sending XML with these attributes to some WS that was not able to handle it. I remember that I was using Apache CXF for sending this XML so I ended up with CXF interceptor that handled removal of these attributes.

I would be happy if anyone gives you a better answer...

Sadly, I didn't find a way how to "disable" generation of these attributes directly in JAXB. What you can do (and probably will be the only way to solve it) is that you take your generated XML and process it once again with another (DOM/SAX) API and remove attributes manually. It is definitely not a nice solution but I'm not sure if you find better :-/

Note