2010-04-08 88 views
0

我有一个相当简单但可能很大的序列化结构。基本XML的结构将是:JAXB可以增加Marshall对象吗?

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

main_object_type可以具有数据的显著量。在我的第一个3500条记录提取中,我必须为JVM提供比应有的更多的内存。

所以,我想写每个(或一堆)main_object_type后磁盘。

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

有什么建议吗?

回答

2

以下情况如何?

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

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class MainObjectType { 
    ... 
} 
0

您可以将您的对象编组为SAX或StAX流。

+0

的@XmlRootElement如果我使用的是流,不会对''仍然需要创建一个'的JAXBElement '的标准方法所有包含在内存中的对象?我正在寻找的是如何分解它,而不必通过编写''头文件等招致大量的手动工作。我想打开''并将每个'写入<他们从DB中检索,没有太多的猴子工作。 – 2010-04-09 15:27:20

+1

尝试使用@XmlID与自定义IDResolver: http://weblogs.java.net/blog/2005/08/15/pluggable-ididref-handling-jaxb-20 您的简单包装将只存储ID对象并通过ID解析器检索对象实例。 – lexicore 2010-04-09 15:39:54