eclipse 如何从 JAXB 编组的 XML 文件中删除 xmlns:xsi 和 xsi:type
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27129969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file
提问by user3572079
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:
我有一组 JAXB 生成的类,其中一些类具有接受“Object”作为参数的 setter 方法。例如:
@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. 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:
在我的代码中创建这些类的实例后,我调用传入所需值的 setter 方法。尽管该方法的参数是 Object,但值最有可能是字符串(我无法控制它的定义方式)。但是,为了保持一致,我将字符串强制转换为 Object,以便它匹配方法的参数类型。代码如下所示:
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:
我在某处读过关于方法的参数类型和传递给它的内容之间的数据类型不匹配的内容。在我的例子中,方法参数是“Object”,但我向它传递了一个字符串(尽管我已经将它转换为 Object)。我也看过这篇文章,它看起来与我的问题相似:
"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
谢谢
回答by lexicore
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.
xsi:type
如果您的数据指定了模型以外的其他类型,则JAXB 会导出。在您的情况下,您设置了一个字符串,但该字段是Object
. 因此,您的数据与模型的类型不同。行为是正确的。
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 anObject
in the first place? - Update:You can use jaxb:javaTypebinding for that.
- ùse
@XmlElementRef
/@XmlMixed
combo instead.
- 让它
String
,为什么它Object
首先是一个? - 更新:您可以为此使用jaxb:javaType绑定。
- ùse
@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? :)
当你将它转换为 object 时,你认为字符串会发生什么?:)
回答by bdoughan
You can always override the property type using the type
parameter on the @XmlElement
annotation.
您始终可以使用注释type
上的参数覆盖属性类型@XmlElement
。
@XmlElement(name = "DefaultCar", type=String.class)
protected Object defaultcar;
回答by zdenda.online
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.
我有类似的问题。我将带有这些属性的 XML 发送到一些无法处理它的 WS。我记得我使用 Apache CXF 来发送这个 XML,所以我最终使用了 CXF 拦截器来处理这些属性的删除。
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 :-/
遗憾的是,我没有找到如何直接在 JAXB 中“禁用”这些属性的生成。您可以做的(并且可能是解决它的唯一方法)是获取生成的 XML 并使用另一个 (DOM/SAX) API 再次处理它并手动删除属性。这绝对不是一个好的解决方案,但我不确定你是否找到更好的:-/
I would be happy if anyone gives you a better answer...
如果有人给你更好的答案,我会很高兴......