JAXB可以递增Marshall一个对象吗? [英] Can JAXB Incrementally Marshall An Object?

查看:59
本文介绍了JAXB可以递增Marshall一个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单但可能很大的序列化结构。基本上XML的结构将是:

I've got a fairly simple, but potentially large structure to serialize. Basically the structure of the XML will be:

<simple_wrapper>
   <main_object_type>
     <sub_objects>
   </main_object_type>
     ... main_object_type repeats up to 5,000 times
</simple_wrapper>

main_object_type 可能有大量的数据。在我的第一个3,500记录提取中,我不得不给JVM提供比它应该需要的更多内存。

The main_object_type can have a significant amount of data. On my first 3,500 record extract, I had to give the JVM way more memory than it should need.

所以,我想在每个之后写出磁盘(或者一堆) main_object_type

So, I'd like to write out to disk after each (or a bunch of) main_object_type.

我知道设置 Marshaller.JAXB_FRAGMENT 会允许它碎片,但是我松开了外部的xml文档标签和< simple_wrapper>

I know that setting Marshaller.JAXB_FRAGMENT would allow it fragments, but I loose the outer xml document tags and the <simple_wrapper>.

有任何建议吗?

推荐答案

以下情况如何?

JAXBContext jaxbContext= JAXBContext.newInstance(MainObjectType.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

OutputStreamWriter writer = new OutputStreamWriter(System.out);

// Manually open the root element
writer.write("<simple_wrapper>");

// Marshal the objects out individually
marshaller.marshal(mainObjectType1, writer);
marshaller.marshal(mainObjectType2, writer);
marshaller.marshal(mainObjectType3, writer);
marshaller.marshal(mainObjectType4, writer);
...

// Manually close the root element
writer.write("</simple_wrapper>");
writer.close();

这假设你在MainObjectType上有一个@XmlRootElement

This assumes you have an @XmlRootElement on MainObjectType

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MainObjectType {
    ...
}

这篇关于JAXB可以递增Marshall一个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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