JAXB将不需要的名称空间声明附加到标记 [英] JAXB appending unneeded namespace declarations to tags

查看:123
本文介绍了JAXB将不需要的名称空间声明附加到标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个自制的XMPP子协议,我正在使用StAX和JAXB的组合来解析/编组消息。当我编组一条消息时,我最终会收到大量不需要的命名空间声明:

I'm implementing a homebrew subprotocol of XMPP, and i'm using combination of StAX and JAXB for parsing/marshalling mesages. And when I marshall a message I end up with loads of unneded namespace declarations:

   <ns2:auth xmlns:ns2="urn:ietf:params:xml:ns:ilf-auth" 
   xmlns:ns4="ilf:iq:experiment:power" xmlns:ns3="ilf:iq:experiment:init" 
   xmlns:ns5="ilf:iq:experiment:values" xmlns:ns6="ilf:iq:experiment:result" 
   xmlns:ns7="ilf:iq:experiment:stop" xmlns:ns8="ilf:iq:experiment:end">
   compton@ilf</ns2:auth>

而不是:

   <ns:auth xmlns:ns="urn:ietf:params:xml:ns:ilf-auth>compton@ilf</ns:auth>

有什么方法可以解决这个问题吗?

Is there any way to turn that of?

所有这些命名空间都用在不同的消息中由JAXB编组/解组,但每条消息都使用一个命名空间。

All these namespaces are used in different messages that get marshalled/unmarshalled by JAXB, but every message uses one namespace.

PS。我不是XML专家,如果我做了一些愚蠢的错误,请不要骂我;)

PS. I am not an XML expert please dont rant me if I did some stupid mistake ;)

推荐答案

您要查找的功能是作为JAXB问题跟踪器的增强功能在 issue 103 。由于JAXB作者发现在实际之前再次遍历对象树太昂贵,因此增强功能被拒绝了序列化开始确定实际需要哪些命名空间。

The functionality you are looking for was requested as an enhancement on the JAXB issue tracker in issue 103. The enhancement was declined, since the JAXB authors find it too expensive to traverse the object tree once more before the actual serialization starts to determine which namespaces are actually required.

虽然相当臃肿,但不必要的命名空间是否定的t使XML文档无效。如果你真的必须保存几个字节并且额外的计算成本是可以承受的,那么编写你自己的XML处理器/过滤器应该很容易,它接受JAXB输出,解析文档并编写一个没有未使用的命名空间定义的新文档。

Although rather bloated, the unnecessary namespaces are not invalidating the XML document. If you really have to save the few bytes and the extra computation cost is affordable, it should be quite easy to write your own XML processor/filter, which takes the JAXB output, parses the document and writes a new document without the unused namespace definitions.

这篇关于JAXB将不需要的名称空间声明附加到标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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