unit XmlInterpreterVisitors; interface uses Classes, XmlInterpreter; type TXmlPrettyPrinter = class(TXmlInterpreterVisitor) private FList : TStringList; FIndent : Integer; function GetText : string; protected procedure AddString(AStr : string); public constructor Create; destructor Destroy; override; procedure Visit(Exp : TXmlStartTag); override; procedure Visit(Exp : TXmlEndTag); override; procedure Visit(Exp : TXmlNode); override; procedure Visit(Exp : TXmlProlog); override; procedure Clear; property Text : string read GetText; end; implementation uses SysUtils; { TXmlPrettyPrinter } const IndentAmount = 4; constructor TXmlPrettyPrinter.Create; begin inherited; FList := TStringList.Create; FIndent := IndentAmount; end; destructor TXmlPrettyPrinter.Destroy; begin FList.Free; inherited; end; function TXmlPrettyPrinter.GetText : string; begin Result := FList.Text; end; procedure TXmlPrettyPrinter.AddString(AStr : string); begin FList.Add(StringOfChar(' ',FIndent) + AStr); end; procedure TXmlPrettyPrinter.Visit(Exp : TXmlStartTag); begin AddString('<' + Exp.TagName + '>'); Inc(FIndent,IndentAmount); end; procedure TXmlPrettyPrinter.Visit(Exp : TXmlEndTag); begin Dec(FIndent,IndentAmount); AddString(''); AddString(''); end; procedure TXmlPrettyPrinter.Visit(Exp : TXmlNode); begin if Exp.Data = '' then begin // Print an empty tag AddString('<' + Exp.StartTag.TagName + '/>'); end else begin AddString(Format('<%s>%s', [Exp.StartTag.TagName, Exp.Data, Exp.EndTag.TagName])); end; end; procedure TXmlPrettyPrinter.Visit(Exp : TXmlProlog); begin AddString(''); AddString(''); end; procedure TXmlPrettyPrinter.Clear; begin FList.Clear; end; end.