2008-10-24 22 views

cevap

15

RTL, XMLDoc.pas içinde dizeleri kabul eden ve döndüren FormatXMLData'a sahiptir.

10

:

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. 

yukarıdaki kod parçası kamu malı salınır. XSLT kullanmak

4

...

<?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> 
+0

PS - Bu için çökecek. – dacracot

1

XML Belge DOM nesnesi Delphi içine inşa oldukça biçimlendirme seçeneği vardır. Sadece XML'inizi içine yüklersiniz ve geri yüklersiniz, ve eğer bu seçeneklere sahipseniz, o zaman her şey güzel olur.

Bakın ve bu cevabı güncelleyin.

+0

Cevap vermeden önce bakmalısın :) –

+0

Her zaman zaman yok, ama güzel olurdu. –

4

Michael Elsdörfer'den libtidy ile Tidy kullandım. Size çok sayıda seçenek sunar ve bunları harici olarak uygulamaya yapılandırabilirsiniz. HTML'ye de uygulanabilir.

Bu, kullandığım bazı çok kaba koddur. Seninle istediğin gibi yap.

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; 
+0

Düzeltme için teşekkürler, Bruce. –

İlgili konular