Pretty-Printing XML: A 'Hello World' Example

REALbasic has no built-in method to pretty-print XML. What it does have is support for XSLT, and that will allow you to generate nicely-formatted XML with no effort on your part other than to learn some XSLT.

XSLT is a language for describing transformations of XML documents. Since I don't understand it yet, I won't explain the transform itself, except to say that it simply copies the input, and indents the output. Here it is, printed prettily.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <xsl:copy-of select="/" />
    </xsl:template>
</xsl:transform>

Let''s suppose that you have this available in your REALbasic project as a String called IndentXML. Then you can format an XML document as follows.

Function PrettyPrint(extends xml as XMLDocument) as String
    return xml.Transform(IndentXML)
End Function