如何在没有命名空间的情况下编组? [英] How to marshal without a namespace?

查看:125
本文介绍了如何在没有命名空间的情况下编组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JAXB创建了一个相当大的重复XML。将整个对象存储在内存中然后进行编组会占用太多内存。基本上,我的XML看起来像这样:

I have a fairly large repetitive XML to create using JAXB. Storing the whole object in the memory then do the marshaling takes too much memory. Essentially, my XML looks like this:

<Store>
  <item />
  <item />
  <item />
.....
</Store>

目前我的解决方案是将根标签硬编码到输出流,并且逐个编组每个重复元素:

Currently my solution to the problem is to "hard code" the root tag to an output stream, and marshal each of the repetitive element one by one:

aOutputStream.write("<?xml version="1.0"?>")
aOutputStream.write("<Store>")

foreach items as item
  aMarshaller.marshall(item, aOutputStream)
end
aOutputStream.write("</Store>")
aOutputStream.close()

不知何故,JAXB生成这样的XML

Somehow the JAXB generate the XML like this

 <Store  xmlns="http://stackoverflow.com">
  <item xmlns="http://stackoverflow.com"/>
  <item xmlns="http://stackoverflow.com"/>
  <item xmlns="http://stackoverflow.com"/>
.....
</Store>

虽然这是一个有效的XML,但它看起来很难看,所以我想知道是否有任何告诉marshaller不要为item元素放置命名空间的方法?或者是否有更好的方法使用JAXB按块大小序列化为XML块?

Although this is a valid XML, but it just looks ugly, so I'm wondering is there any way to tell the marshaller not to put namespace for the item elements? Or is there better way to use JAXB to serialize to XML chunk by chunk?

推荐答案

以下为我做了诀窍:

         XMLStreamWriter writer = ...
         writer.setNamespaceContext(new NamespaceContext() {
            public Iterator getPrefixes(String namespaceURI) {
                return null;
            }

            public String getPrefix(String namespaceURI) {
                return "";
            }

            public String getNamespaceURI(String prefix) {
                return null;
            }
        });

这篇关于如何在没有命名空间的情况下编组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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