Is there an xml equivalent for the JSON formatting methods in apex?

Is there any method to take an xml string or a DOM.document object and get a nicely formatted string of the xml (like serializepretty does for JSON)?

I’m fairly certain the answer is no but just putting this up in case someone else runs into the same requirement.

Answer

Not per se, but via the DOM.Document class you could whip up an XML pretty-print method in a few minutes. Contrary to what Andrew Fawcett said here, I don’t think it would be all that costly; you could probably get a full pretty print with a max of 2-3 script statements per XML element. We do some pretty hardcore XML parsing and processing in our app (much more script-intensive than pretty printing) and haven’t run into script limits yet.

Attribution
Source : Link , Question Author : Greg Grinberg , Answer Author : jkraybill

Leave a Comment