Nice bit of code to format an xml string

前端 未结 5 1048
轮回少年
轮回少年 2021-02-05 12:55

Anyone got a ready made function that will take an XML string and return a correctly indented string?

eg

A

        
5条回答
  •  梦谈多话
    2021-02-05 13:40

    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('A'));
      Readln;
    end.
    

    The code fragment above is released to public domain.

提交回复
热议问题