XML Node to String Conversion for Large Sized XML

budz01

New Member
Till now I was using DOMSource to transform the XML file into string. Here's my code : \[code\]public String convertElementToString (Node element) throws TransformerConfigurationException, TransformerFactoryConfigurationError{ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(element); try { transformer.transform(source, result); } catch (TransformerException e) { Log.e("CONVERT_ELEMENT_TO_STRING", "converting element to string failed. Aborting", e); } String xmlString = result.getWriter().toString(); xmlString = xmlString.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""); xmlString = xmlString.replace("\n", ""); return xmlString; }\[/code\]This was working fine for small xml files. But for large sized xml this code started throwing OutOfMemoryError.In my research I found that I have to use SAXSource (or StAX) for the transformation for large xml. But, I was not able to find out how to implement it.
 
Top