Hi,
ich parse eine xml-Datei mit einer xsl Datei, bei beiden kann ich die Struktur nicht ändern. Das xml File ist 350MB groß. Ich bekomme hierbei einen out of Memory Fehler. Ich habe erst JDOM verwendet und habe gelsen das Sax weniger Speicher benötigt, jedoch erhalte ich auch unter bei dieser Verwendung den Fehler. Wenn ich der JVM mehr als 2 Gbyte zuweise geht es vermutlich, aber ich hätte es gerne etwas reduziert. Habt Ihr eine Idee, hier der Quelltext:
public void convertXMLwithSAX(String inFilename, String outFilename, String xslFilename) throws SAXException, TransformerConfigurationException, IOException {
//Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports The use of SAXSource
// and SAXResult
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
{
// Cast the TransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create a ContentHandler to handle parsing of the stylesheet.
TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler();
// Create an XMLReader and set its ContentHandler.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(templatesHandler);
// Parse the stylesheet.
reader.parse(xslFilename);
//Get the Templates object from the ContentHandler.
Templates templates = templatesHandler.getTemplates();
// Create a ContentHandler to handle parsing of the XML source.
TransformerHandler handler
= saxTFactory.newTransformerHandler(templates);
// Reset the XMLReader's ContentHandler.
reader.setContentHandler(handler);
// Set the ContentHandler to also function as a LexicalHandler, which
// includes "lexical" events (e.g., comments and CDATA).
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
FileOutputStream fos = new FileOutputStream(outFilename);
java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
Serializer serializer = SerializerFactory.getSerializer(xmlProps);
serializer.setOutputStream(fos);
// Set the result handling to be a serialization to the file output stream.
Result result = new SAXResult(serializer.asContentHandler());
handler.setResult(result);
// Parse the XML input document.
reader.parse(inFilename);
System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\* The result is in xml file \*\*\*\*\*\*\*\*\*\*\*\*\*");
}
else
System.out.println("The TransformerFactory does not support SAX input and SAX output");
}