Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 0 down vote accepted

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.

share|improve this answer

It seems not, I've had a look through the Apex supporting classes for XML for a similar feature to that of the JSON classes, but I cannot see one either. So from my prespective the answer to your question is a no.

That said, though I would not recommend it, as from a statement governor perspective it could get quite expensive. The nearest thing might be using XmlStreamReader and XmlStreamWriter together, and inject the required white space and padding during the read loop, then call getXmlString on the writer to get the XML.

If your asking from a debug perspective, then this site might be help,

share|improve this answer

I guess there is not a general toolkit can handle any XML format.

You may know The Toolkit for Google Data APIs

There is a class called XMLDom that may give you some idea of how to handle the XML format in Apex.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.