Skip to content Skip to sidebar Skip to footer

XML Node To String Conversion For Large Sized XML

Till now I was using DOMSource to transform the XML file into string, in my Android App. Here's my code : public String convertElementToString (Node element) throws TransformerCo

Solution 1:

First off: if you just need the XML as a string, and aren't using the Node for anything else, you should use StAX (Streaming API for XML) instead, as that has a much lower memory footprint. You'll find StAX in the javax.xml.stream package of the standard libraries.

One improvement to your current code would be to change the line

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

to

transformer.setOutputProperty(OutputKeys.INDENT, "no");

Since you're stripping newlines anyway at the end of the method, it's not very useful to request additional indentation in the output. It's a small thing, but might reduce your memory requirements a bit if there are a lot of tags (hence, newlines and whitespace for indentation) in your XML.


Post a Comment for "XML Node To String Conversion For Large Sized XML"