简体   繁体   English

很好的代码来格式化xml字符串

[英]Nice bit of code to format an xml string

Anyone got a ready made function that will take an XML string and return a correctly indented string? 任何人都有一个现成的函数,它将采用XML字符串并返回正确缩进的字符串?

eg 例如

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

and will return nicely formatted String in return after inserting linebreaks and tabs or spaces? 并在插入换行符和制表符或空格后返回格式良好的String?

RTL在XMLDoc.pas中具有FormatXMLData ,用于接受和返回字符串。

Using OmniXML : 使用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... 使用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>

I have used Tidy with libtidy from Michael Elsdörfer. 我和MichaelElsdörfer使用了Tidy和libtidy。 It give you heaps of options and you can configure them externally to the application. 它为您提供了大量选项,您可以在应用程序外部配置它们。 Also applicable to HTML. 也适用于HTML。

This is some very rough code that I used. 这是我使用的一些非常粗略的代码。 Do with it as you please. 随你随心所欲。

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;

The XML Document DOM object build into Delphi has a pretty formatting option. 构建到Delphi中的XML Document DOM对象有一个非常格式化的选项。 You just load your XML into it and save it back out, and if you have that option set then it makes it all pretty. 您只需将XML加载到其中并将其保存回来,如果您设置了该选项,那么它就会非常漂亮。

I'll look it up and update this answer. 我会查一下并更新这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM