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

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

问题描述

我有一个相当大的重复 XML 需要使用 JAXB 创建.将整个对象存储在内存中然后进行编组需要太多内存.本质上,我的 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,但它看起来很难看,所以我想知道有什么方法可以告诉编组器不要为 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天全站免登陆