I tried to create file for test with 10 000 000 nodes like:
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);
But received error:
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)
Does there exist another library for create XML files with more than 10 000 000 nodes in Java?
For trivial files like that: consider writing the xml file without using any DOM or StAX:
That’s all – you just need a method that writes a String to a file. And a method to get your textual data.