在使用JAXB封送泛型对象时如何摆脱命名空间 [英] How to get rid of namespace when marshaling generic objects with JAXB

查看:218
本文介绍了在使用JAXB封送泛型对象时如何摆脱命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码序列化一个类:

I use the following code to serialize a class:

public String serialize(T oObject)
{
    mMarshaller = getJAXBContext().createMarshaller();
    mMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream strm = getOutputStream();
    mMarshaller.marshal(oObject, strm);
    return strm.toString();
}

但是当我查看generaetd XML时,那里有一个名称空间:

But when I look at the generaetd XML there is a namespace in there:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapEntry>
    <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">
    Key
    </key>
    <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">
     oValue
     </value>
 </mapEntry>

有没有办法删除它,或告诉JAXB它不应该添加这个?我整个上午都在看这个,并尝试了一些我通过谷歌找到的东西,但没有任何帮助。

Is there some way to remove this, or tell JAXB that it should NOT add this? I'm looking at this now the whole morning and tried several things I found via google, but nothing helped.

现在我在这里找到了这个帖子:如何在没有命名空间的情况下编组?但问题是接受的答案只是部分列出现在我不知道这是否会对我有所帮助。 XMLStreamWriter 是一个接口,我不想为此实现整个流编写器。那么是否有一些方法可以扩展 ByteArrayOutputStream 而无需实现所有其他函数,例如XMLWriter需要?

Now I found this thread here: How to marshal without a namespace? but the problem is that the accepted answer is only partially listed and now I don't know if this would help me. XMLStreamWriter is an interface, and I don't want to implement a whole stream writer just for that. So is there some way to extend the ByteArrayOutputStream without the need of implementing all the other functions, such an XMLWriter would need?

推荐答案

在本用例中, http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema 因为 xsi:type 属性而引入名称空间。由于您的JAXB实现认为属性类型为 Object ,因此引入了 xsi:type 属性。解决方案是确保不键入属性 Object

In this use case the http://www.w3.org/2001/XMLSchema-instance and http://www.w3.org/2001/XMLSchema namespaces are being brought in because of the xsi:type attribute. The xsi:type attribute is being brought in because your JAXB implementation believes the property type to be Object. The solution is to ensure the properties aren't typed Object.

XML表示看起来像表示的一部分for java.util.Map (参见: http://blog.bdoughan.com/2013/03/jaxb-and-javautilmap.html )。这是你的用例还是你有另一个对象模型?

The XML representation looks like part of the representation for java.util.Map (see: http://blog.bdoughan.com/2013/03/jaxb-and-javautilmap.html). Is this your use case or do you have another object model?

这篇关于在使用JAXB封送泛型对象时如何摆脱命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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