2010-04-08 91 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