JAXB可以增量地封送对象吗?

JAXB可以增量地封送对象吗?,jaxb,marshalling,Jaxb,Marshalling,我有一个相当简单但可能很大的结构要序列化。XML的结构基本上是: <simple_wrapper> <main_object_type> <sub_objects> </main_object_type> ... main_object_type repeats up to 5,000 times </simple_wrapper> ... 主对象类型最多可重复5000次 main\u object

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

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

... 主对象类型最多可重复5000次
main\u object\u type
可以包含大量数据。在我的第一次3500记录提取中,我必须给JVM提供比它应该需要的更多的内存

因此,我想在每个(或一组)主对象类型之后将其写入磁盘

我知道设置
Marshaller.JAXB_FRAGMENT
会允许它产生片段,但我松开了外部xml文档标记和


有什么建议吗?

您可以将对象封送到SAX或StAX流中。

以下内容如何

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();

如果我使用流,为
创建
JAXBElement
的标准方法是否仍然需要将包含的所有对象保存在内存中?我要寻找的是如何在不需要大量手工编写
标题等的情况下将其分解。我想打开
并在从数据库检索时写入每个
,不要做太多的冒充工作。尝试将@XmlID与自定义IDResolver一起使用:您的简单包装器将只存储对象的id,并通过id解析器检索对象实例。
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MainObjectType {
    ...
}