我試圖創建的測試文件中包含10個000 000節點:創建具有大量節點(10萬美元)的XML文件
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("root");
document.appendChild(rootElement);
for (int i = 1; i <= 10000000; i++) {
Element em = document.createElement("ch");
em.appendChild(document.createTextNode("ch_data"));
rootElement.appendChild(em);
}
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("c:/file1.xml"));
transformer.transform(source, result);
但收到的錯誤:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl. createElement(CoreDocumentImpl.java:620) at main.CreatXMLFile.main(CreatXMLFile.java:27)
是否存在另一個用於在Java中創建超過10萬個節點的XML文件的庫?
出於好奇,你爲什麼要創建如此龐大的XML文件?假設每個節點將被存儲爲約60個字節,則該文件將超過600MB(這是decimail MB,而不是二進制MiB),這對於平面文件來說是巨大的尺寸。如果你打算在這個文件中存儲的東西不會是一個更好的解決方案的數據庫? –