简体   繁体   English

如何从JAXB编组的XML文件中删除xmlns:xsi和xsi:type

[英]How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file

I've got a set of JAXB generated classes and some of the classes have setter methods which accepts "Object" as the parameter. 我有一组JAXB生成的类,有些类有setter方法,它接受“Object”作为参数。 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;
}

After I've created instances of these classes in my code, I call the setter methods passing in the required value. 在我的代码中创建了这些类的实例后,我调用setter方法传递所需的值。 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). 虽然方法的参数是Object,但值最有可能是字符串(我无法控制它的定义方式)。 However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type. 但是,为了保持一致,我将字符串转换为Object,以便它与方法的参数类型匹配。 The code looks something like this: 代码看起来像这样:

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

When I marshall the Java objects, I get the following within the resulting XML, just in front of the value of the string: 当我编写Java对象时,我在生成的XML中得到以下内容,就在字符串的值前面:

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" and "xmlns:xsi" in generated xml by JAXB JAXB生成的xml中的“xsi:type”和“xmlns:xsi”

However, it doesn't help me overcome my problem. 但是,它无助于我解决我的问题。 Is there a way to remove these references to xmlns:xsi and xsi:type? 有没有办法删除这些对xmlns的引用:xsi和xsi:type?

Thx 谢谢

JAXB exports xsi:type if your data specifies other type than your model. 如果您的数据指定的是除模型之外的其他类型,则JAXB将导出xsi:type In your case, you set a string, but the field is Object . 在您的情况下,您设置一个字符串,但该字段是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: 有很多方法可以实现这一目标:

  • Make it String , why is it an Object in the first place? 使它成为String ,为什么它首先是一个Object
  • Update: You can use jaxb:javaType binding for that. 更新:您可以使用jaxb:javaType绑定。
  • Ùse @XmlElementRef / @XmlMixed combo instead. 使用@XmlElementRef / @XmlMixed组合来代替。

However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type. 但是,为了保持一致,我将字符串转换为Object,以便它与方法的参数类型匹配。

What do you think happens to the string when you cast it to object? 当你将它转换为对象时,你认为字符串会发生什么? :) :)

You can always override the property type using the type parameter on the @XmlElement annotation. 您始终可以使用@XmlElement批注上的type参数覆盖属性类型。

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

I had similar problem. 我有类似的问题。 I was sending XML with these attributes to some WS that was not able to handle it. 我将带有这些属性的XML发送给一些无法处理它的WS。 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. 我记得我使用Apache CXF发送这个XML,所以我最终得到了CXF拦截器来处理这些属性的删除。

Sadly, I didn't find a way how to "disable" generation of these attributes directly in JAXB. 遗憾的是,我没有找到一种方法如何在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. 您可以做什么(并且可能是解决问题的唯一方法)是您使用生成的XML并使用另一个(DOM / SAX)API再次处理它并手动删除属性。 It is definitely not a nice solution but I'm not sure if you find better :-/ 这绝对不是一个很好的解决方案,但我不确定你是否找到了更好的: - /

I would be happy if anyone gives you a better answer... 如果有人给你更好的答案我会很高兴...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM