如何从JAXB编组的XML文件中删除xmlns:xsi和xsi:type [英] How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file

查看:1120
本文介绍了如何从JAXB编组的XML文件中删除xmlns:xsi和xsi:type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组JAXB生成的类,有些类有setter方法,它接受
Object作为参数。例如:

I've got a set of JAXB generated classes and some of the classes have setter methods which accepts "Object" as the parameter. For example:

@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;
}

在我的代码中创建了这些类的实例后,我调用setter方法传递所需的值。虽然方法的参数是Object,但值很可能是字符串(我没有但是,为了保持一致,我将字符串转换为Object,使其与方法的参数类型匹配。代码如下所示:

After I've created instances of these classes in my code, I call the setter methods passing in the required value. Although the method's parameter is Object, the values are most likely to be strings (I've got no control over how it is defined). However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type. The code looks something like this:

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



<当我编写Java对象时,我在结果XML中得到以下内容,就在字符串的值前面:

When I marshall the Java objects, I get the following within the resulting XML, just in front of the value of the string:

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

我读过某个地方方法的参数类型与传递给它的内容之间的数据类型不匹配。在我的情况下,方法参数是对象,但我传入一个字符串给它(虽然我把它转换为对象)。我也看过这篇帖子,看起来和我的问题类似:

I've read somewhere about mismatching data types between the method's parameter type and what has been passed to it. In my case the method parameter being "Object" but I'm passing in a string to it (although I've cast it to Object). I've also seen this post and it looks similar to my problem:

"的xsi:type"和xmlns:xsi;在JAXB生成的xml中

然而,它并没有帮助我克服我的问题。有没有办法删除这些对xmlns的引用:xsi和xsi:type?

However, it doesn't help me overcome my problem. Is there a way to remove these references to xmlns:xsi and xsi:type?

Thx

推荐答案

如果您的数据指定的是除模型之外的其他类型,则JAXB会导出 xsi: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.

如何解决这个问题。您已将属性的类型与数据类型对齐。有很多方法可以实现这一目标:

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:


  • 让它字符串,为什么它首先是对象

  • 更新:您可以使用 jaxb:javaType 绑定。

  • Ùse @XmlElementRef / @XmlMixed 改为组合。

  • Make it String, why is it an Object in the first place?
  • Update: You can use jaxb:javaType binding for that.
  • Ùse @XmlElementRef/@XmlMixed combo instead.

但是,为了保持一致,我将字符串转换为Object,所以
它与方法的参数类型匹配。

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

当你将它转换为对象时,你认为字符串会发生什么? :)

What do you think happens to the string when you cast it to object? :)

这篇关于如何从JAXB编组的XML文件中删除xmlns:xsi和xsi:type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆