【问题标题】:Nice bit of code to format an xml string格式化xml字符串的好代码
【发布时间】:2010-09-19 01:01:33
【问题描述】:

任何人都有一个现成的函数,它将接受一个 XML 字符串并返回一个正确缩进的字符串?

例如

<XML><TAG1>A</TAG1><TAG2><Tag3></Tag3></TAG2></XML>

并在插入换行符和制表符或空格后返回格式良好的字符串?

【问题讨论】:

    标签: xml delphi pretty-print text-formatting


    【解决方案1】:

    使用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.
    

    以上代码片段已发布到公共领域。

    【讨论】:

      【解决方案2】:

      使用 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>
      

      【讨论】:

      • PS - 这会将 折叠为 。
      【解决方案3】:

      在 Delphi 中构建的 XML 文档 DOM 对象有一个漂亮的格式化选项。您只需将您的 XML 加载到其中并将其保存回来,如果您设置了该选项,那么它会变得很漂亮。

      我会查找并更新此答案。

      【讨论】:

      • 你应该在回答之前查一下 :)
      • 不总是有时间,但会很好。
      【解决方案4】:

      RTL 在 XMLDoc.pas 中有FormatXMLData,它接受并返回字符串。

      【讨论】:

        【解决方案5】:

        我使用了 Tidy 和来自 Michael Elsdörfer 的 libtidy。它为您提供了大量选项,您可以在应用程序外部配置它们。也适用于 HTML。

        这是我使用的一些非常粗略的代码。随心所欲。

        function TForm1.DoTidy(const Source: string): string;
        var
          Tidy              : TLibTidy;
        begin
          if not TidyGlobal.LoadTidyLibrary('libtidy.dll') then
          begin
            //    Application.MessageBox('TidyLib is not available.', 'Error', 16);
            //    exit;
            raise Exception.Create('Cannot load TidyLib.dll');
          end;
          Tidy := TLibTidy.Create(Self);
          try
            Tidy.LoadConfigFile(ExtractFilePath(Application.ExeName) +
              'tidyconfig.txt');
            //    Tidy.Configuration.IndentContent := tsYes;
            //    Tidy.Configuration.IndentSpaces := 5;
            //    Tidy.Configuration.UpperCaseTags := False;
            //    Tidy.Configuration.NumEntities := True;
            //    Tidy.Configuration.AccessibilityCheckLevel := 2;
            //    Tidy.Configuration.InlineTags := 'foo,bar';
            //    Tidy.Configuration.XmlDecl := True;
            //    Tidy.Configuration.XmlTags := True;
            //    Tidy.Configuration.CharEncoding := TidyUTF8;
            //    Tidy.Configuration.WrapLen := 0;
            //    Tidy.SaveConfigFile('tidyconfig.txt');
            Tidy.ParseString(Source);
            Result := Tidy.RunDiagnosticsAndRepair;
          finally
            Tidy.Free;
          end;
        end;
        

        【讨论】:

          猜你喜欢
          • 2010-11-10
          • 2011-04-06
          • 2023-03-27
          • 1970-01-01
          • 1970-01-01
          • 2020-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多