Using Delphi7 TClientDataSet: is it possible to have it save it's XML contents in an indented format

心不动则不痛 提交于 2019-12-04 10:58:30
François

It's because the proper encoding (like <?xml version="1.0" encoding="UTF-8"?>) has not be specified in your output file, yet it contains some characters with an incompatible encoding.

As RRUZ mentioned, specifying explicitly the TDataPacketFormat as dfXMLUTF8 when writing the file will most certainly solve the 'Invalid Character' error, as it will write the encoding tag first:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DATAPACKET Version="2.0">[...]
You can also add the encoding manually at the beginning of the file for already existing files.

As for the readable formatting, some readers can read the raw one-liner and do the formatting for you (browsers like FireFox or Internet Exporer, and XML editors like XMLNotePad)

When you uses the TCustomClientDataSet.SaveToFile procedure, you can choose the output format, for default this value is set to dfBinary wich encode the data in a binary format.

 procedure TCustomClientDataSet.SaveToFile(const FileName: string = '';
  Format: TDataPacketFormat = dfBinary);

try changing the Format parameter to dfXML or dfXMLUTF8

ClientDataSet1.SaveToFile('file.xml',dfXML);

if you want format the XML output you can use the FormatXMLData function try this code

uses
 XMLDoc;

Procedure FormatXMLFile(XmlFile:string);
var
   oXml : TXMLDocument;
 begin
   oXml := TXMLDocument.Create(nil);
   try
     oXml.LoadFromFile(XmlFile);
     oXml.XML.Text:=xmlDoc.FormatXMLData(oXml.XML.Text);
     oXml.Active := true;
     oXml.SaveToFile(XmlFile);
   finally
     oXml := nil;
   end;
 end;

finally you code will look like this

 ClientDataSet1.SaveToFile('test.xml',dfXML);
 FormatXMLFile('test.xml');
Wellington Telles Cunha

I modified your code, because I had some problems with UTF-8:

Procedure FormatXMLFile(XmlFile:string);
var
   oXml : TXMLDocument;
   s : utf8String;
begin
   oXml := TXMLDocument.Create(nil);
   try
     oXml.LoadFromFile(XmlFile);
     s :=  oxml.XML.Text;
     s  := StringReplace(s, '><', '>' + #13#10 + '<' , [rfReplaceAll]);
     //oXml.XML.Text:=xmlDoc.FormatXMLData(oxml.XML.Text);
     oxml.XML.Text := s;
     oXml.Active := true;
     oXml.SaveToFile(XmlFile);
   finally
     oXml := nil;
   end;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!