Nice bit of code to format an xml string Nice bit of code to format an xml string xml xml

Nice bit of code to format an xml string


The RTL has FormatXMLData in XMLDoc.pas that accepts and returns strings.


Using OmniXML:

program TestIndentXML;{$APPTYPE CONSOLE}uses  SysUtils,  OmniXML,  OmniXMLUtils;function IndentXML(const xml: string): string;var  xmlDoc: IXMLDocument;begin  Result := '';  xmlDoc := CreateXMLDoc;  if not XMLLoadFromAnsiString(xmlDoc, xml) then    Exit;  Result := XMLSaveToAnsiString(xmlDoc, ofIndent);end;begin  Writeln(IndentXML('<XML><TAG1>A</TAG1><TAG2><Tag3></Tag3></TAG2></XML>'));  Readln;end.

The code fragment above is released to public domain.


Using XSLT...

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