OutOfMemoryError occured
hi,
here in my code i am using SAX to read and DOM to write.
i am passing destination file path and the document object.
i am trying to save xml file 50,000 subroot records with in the root in this fashion.
<Root>
<subroot>
<FirstElement>1</FirstElement>
<SecondElement>2</SecondElement>
<ThirdElement>3</thirdElement>
<ForthElement>4</ForthElement>
<FifthElement>5</FifthElement>
</subroot>
</root>
while transforming DOMSource to StreamResult i got OutOfMemoryError.
here is the following code.
can any one help.
privatevoid transformAndSave(File theDestinationFile, Document theDocument)
throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException, IOException{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
try{
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
transformer.setOutputProperty(OutputKeys.STANDALONE,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
}catch (Exception e){
// We don't want to fail just because we are unable to set a property (incase we are not using the default one).
// So, just ignore any exceptions thrown.
}
// Create a StringWriter to store the transformed result (XML document)
StringWriter stringWriter =new StringWriter();
StreamResult streamResult =new StreamResult(stringWriter);
// Create a Source object for the Document which we want to transform.
DOMSource source =new DOMSource(theDocument);
transformer.transform(source, streamResult);
// here i have encounterd (OutOfMemoryError)
// Now we have the XML contents in the StringWriter (which we passed to the StreamResult)
// Write the XML contents to the file now.
BufferedWriter bufferedWriter =new BufferedWriter(new FileWriter(theDestinationFile));
bufferedWriter.write(stringWriter.toString());
bufferedWriter.flush();
// Close the Writer now.
try{
bufferedWriter.close();
}catch (Exception e){
// We don't want to throw an exception if the close() fails.
// Just ignore the exception thrown during close()
}
}
thank you

