3
直到現在我用DOMSource的 XML文件轉換成字符串,在我的Android應用程序。XML節點至字符串轉換爲冬季大XML
這裏是我的代碼:
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;
}
這是爲小型XML文件做工精細。
但爲大型XML這個代碼開始拋出的OutOfMemoryError。
什麼可能是背後的原因以及如何糾正這個問題?
有多大XML? – 2013-04-09 11:34:15
它可能會達到10-15 MB。 – 2013-04-10 04:21:20