How to remove xmlns: xsi and xsi: enter a marshalled XML file from JAXB

I have a set of JAXB generated classes, and some of the classes have setter methods that take an "Object" as a parameter. For instance:

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name="Car", propOrder = { "defaultCar" } public class Car { @XmlElement(name = "DefaultCar") protected Object defaultcar; public void setDefaultCar(Object value) { this.defaultCar = value; } 

After I instantiate these classes in my code, I call the setter methods that pass at the required value. Although the method parameter is Object, the values ​​are likely to be strings (I have no control over how it is defined). However, to preserve the integrity, I passed the Object string to match the type of the method parameter. The code looks something like this:

  Object value = "Old Banger"; Method method = aCar.getClass().getMethod("setDefaultCar", Object.class); method.invoke(aCar, value); 

When I sort Java objects, I get the following in the resulting XML, right before the string value:

  xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: type = "xs: string" 

I read somewhere about the mismatch of data types between the type of a method parameter and what was passed to it. In my case, the method parameter is "Object", but I pass it a string (although I passed it Object). I also saw this post, and it looks like my problem:

"xsi: type" and "xmlns: xsi" in the generated xml from JAXB

However, this does not help me overcome my problem. Is there a way to remove these links to xmlns: xsi and xsi: type?

thanks

+6
source share
3 answers

JAXB exports xsi:type if your data indicates a type other than your model. In your case, you specify a string, but an Object field. So your data is of a different type than your model. The behavior is correct.

How can you fix it. You map a property type to a data type. There are quite a few ways to achieve this:

  • Make it String , why is it Object first?
  • Update: You can use jaxb: javaType for this.
  • ξse @XmlElementRef / @XmlMixed instead.

However, in order for everything to be consistent, I passed an Object string, so that it matches the type of the method parameter.

What do you think happens to a line when you throw it on an object? :)

+6
source

You can always override a property type by using the type parameter in the @XmlElement annotation.

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

I had a similar problem. I was sending XML with these attributes to some WS that was unable to process it. I remember that I used Apache CXF to send this XML, so I ended up in a CXF interceptor that handled the removal of these attributes.

Unfortunately, I did not find a way to “disable” the generation of these attributes directly in JAXB. What you can do (and probably the only way to solve it) is that you take the generated XML and process it again using another (DOM / SAX) API and delete the attributes manually. This is definitely not a good solution, but I'm not sure if you will find better: - /

I would be glad if someone gives you a better answer ...

0
source

Source: https://habr.com/ru/post/978684/


All Articles