我有以下问题:
我有一个包含5000个随机生成的演示数据集的表
我现在想在Xliff文件中导出这一行。
所以最终结果应该是这样的。小例子
<?xml version='1.0' encoding='utf-8'?>
<xliff version="1.1">
<file original="source\simple.htm" source-language="EN" target-language="DE" datatype="html">
<header>
<skl>
<external-file uid="017dbcf0-c82c-11e2-ba2b-005056c00008" href="skl\simple.htm.skl"/>
</skl>
</header>
<body>
<trans-unit id="00QlKZsL6GyW6B92Ev81Fb2HP3Z0ma">
<source xml:lang="EN">2hH3khJvy1gmFNTLSB0Crka0A8TTKReuYxbV2hI9E8AjXwCV3F</source>
<target xml:lang="DE">3ydQZWavaKvxrlbh1ewXZakLL00LEPG6zVTyty6fiLrPdx9UE4</target>
<note/></trans-unit>
<trans-unit id="016ynILnditynwtYwcl6vJPTLCzvo7">
<source xml:lang="EN">dyC28VRCI9O37PTHENinp4sgMkr5R0HO1Yo53hUQKNr4GoLFG4</source>
<target xml:lang="DE">sEkgstffmS4k5KB1JZkNSYbUnzzlFBNT30oYmtfId8dnspG3No</target>
<note>Testnotiz</note></trans-unit>
<trans-unit id="03YNBAZ1YWvkqaG4PRxKSiWENOCXuB">
<source xml:lang="EN">BHpY8LDs8oJAr8I1EfZzeJX24GZ3TLIr9GUAYcnSPYHjDfKRqk</source>
<target xml:lang="DE">7Rd7bW2lg2Uc4uStCoosZuNgOzA9qWN7OsvW2gBcHa3ctnmF3Q</target>
<note/></trans-unit>
</body>
</file>
</xliff>
作为编辑/粘贴Xliff的一个组件,我想抓住我的XMLDocument文件。我几天前已经写了一个演示程序,我使用XMLDocument上传文件,然后Xliff再次写了一些东西。所以这些例程至少对我已经拥有的目标而言。
我现在感觉更多,我仍然不知道如何将MySQL表中的所有数据作为Xliff文件包中最好的一块。
首先想到的是我可以逐行浏览整个表格,然后将其保存到数组中,然后将我的循环写在数组和文件中。
会欣赏其他一些建议/概念。由于它最终是对XMLDocument组件速度的测试,因此我会选择导致快速课程的概念/想法。
答案 0 :(得分:2)
使用Strategy Design Pattern为各种导出策略(XML Plain或XmlDoc,CSV,...)提供易于扩展的解决方案的解决方案
基础文件
unit Document;
interface
type
TDocument = class;
IDocumentExportFileStrategy = interface
['{787B60E5-A3CA-485C-A46E-248A43D7175C}']
procedure ExportDoc( AContext : TDocument; const AFileName : string );
end;
TDocument = class
private
FExportFileStrategy : IDocumentExportFileStrategy;
protected
function GetValue( const Name : string ) : Variant; virtual; abstract;
public
procedure First; virtual; abstract;
procedure Next; virtual; abstract;
function Eof : Boolean; virtual; abstract;
property Value[const Name : string] : Variant read GetValue;
property ExportFileStrategy : IDocumentExportFileStrategy read FExportFileStrategy write FExportFileStrategy;
procedure ExportTo( const AFileName : string );
end;
implementation
{ TDocument }
procedure TDocument.ExportTo( const AFileName : string );
begin
FExportFileStrategy.ExportDoc( Self, AFileName );
end;
end.
Xliff导出策略编写纯文本以便快速执行
unit XliffPlainExporter;
interface
uses
Document,
SysUtils, Variants,
Classes;
type
TXliffPlainExporter = class( TInterfacedObject, IDocumentExportFileStrategy )
private
procedure WriteLine( AStream : TStream; ALine : string );
protected
procedure WriteHead( AContext : TDocument; AStream : TStream );
procedure WriteDetails( AContext : TDocument; AStream : TStream );
procedure WriteFoot( AContext : TDocument; AStream : TStream );
public
procedure ExportDoc( AContext : TDocument; const AFileName : string );
end;
implementation
{ TXliffPlainExporter }
procedure TXliffPlainExporter.ExportDoc( AContext : TDocument; const AFileName : string );
var
LStream : TStream;
LFileName : string;
begin
AContext.First;
if not AContext.Eof
then
begin
LFileName := AFileName;
if ExtractFileExt( LFileName ) = ''
then
LFileName := ChangeFileExt( LFileName, '.xml' );
LStream := TFileStream.Create( LFileName, fmCreate );
try
WriteHead( AContext, LStream );
WriteDetails( AContext, LStream );
WriteFoot( AContext, LStream );
finally
LStream.Free;
end;
end;
end;
procedure TXliffPlainExporter.WriteHead( AContext : TDocument; AStream : TStream );
begin
WriteLine( AStream, '<?xml version=''1.0'' encoding=''utf-8''?>' );
WriteLine( AStream, '<xliff version="1.1">' );
WriteLine( AStream, ' <file original="source\simple.htm" source-language="EN" target-language="DE" datatype="html">' );
WriteLine( AStream, ' <header>' );
WriteLine( AStream, ' <skl>' );
WriteLine( AStream, ' <external-file uid="017dbcf0-c82c-11e2-ba2b-005056c00008" href="skl\simple.htm.skl"/>' );
WriteLine( AStream, ' </skl>' );
WriteLine( AStream, ' </header>' );
WriteLine( AStream, ' <body>' );
end;
procedure TXliffPlainExporter.WriteDetails( AContext : TDocument; AStream : TStream );
begin
while not AContext.Eof do
begin
WriteLine( AStream, Format( ' <trans-unit id="%s">', [VarToStr( AContext.Value['id'] )] ) );
WriteLine( AStream, Format( ' <source xml:lang="EN">%s</source>', [VarToStr( AContext.Value['src'] )] ) );
WriteLine( AStream, Format( ' <target xml:lang="DE">%s</target>', [VarToStr( AContext.Value['dst'] )] ) );
WriteLine( AStream, ' <note/></trans-unit>' );
AContext.Next;
end;
end;
procedure TXliffPlainExporter.WriteFoot( AContext : TDocument; AStream : TStream );
begin
WriteLine( AStream, ' </body>' );
WriteLine( AStream, ' </file>' );
WriteLine( AStream, '</xliff>' );
end;
procedure TXliffPlainExporter.WriteLine( AStream : TStream; ALine : string );
var
LLine : TStream;
begin
LLine := TStringStream.Create( ALine + sLineBreak, TEncoding.UTF8 );
try
LLine.Position := 0;
AStream.CopyFrom( LLine, LLine.Size );
finally
LLine.Free;
end;
end;
end.
简单测试文档仅用于测试目的
unit TestDocument;
interface
uses
Document;
type
TTestDocument = class( TDocument )
private
FIndex : Integer;
protected
function GetValue( const Name : string ) : Variant; override;
public
function Eof : Boolean; override;
procedure First; override;
procedure Next; override;
end;
implementation
uses
SysUtils,
StrUtils;
{ TTestDocument }
function TTestDocument.Eof : Boolean;
begin
Result := FIndex >= 100;
end;
procedure TTestDocument.First;
begin
inherited;
FIndex := 0;
end;
function TTestDocument.GetValue( const Name : string ) : Variant;
begin
case IndexText( Name, ['id', 'src', 'dst'] ) of
0 :
Result := Format( 'id%8.8d', [FIndex + 1] );
1 :
Result := Format( 'src%8.8d', [FIndex + 1] );
2 :
Result := Format( 'dst%8.8d', [FIndex + 1] );
end;
end;
procedure TTestDocument.Next;
begin
inherited;
Inc( FIndex );
end;
end.
将各个部分放在一起
program DataExportStrategy;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Document in 'Document.pas',
TestDocument in 'TestDocument.pas',
XliffPlainExporter in 'XliffPlainExporter.pas';
procedure Test;
var
LDoc : TDocument;
begin
LDoc := TTestDocument.Create;
try
LDoc.ExportFileStrategy := TXliffPlainExporter.Create;
LDoc.ExportTo( 'test' );
finally
LDoc.Free;
end;
WriteLn( 'Export finished' );
end;
begin
try
Test;
except
on E : Exception do
WriteLn( E.ClassName, ': ', E.Message );
end;
ReadLn;
end.
在您的情况下,您希望拥有一个基于DataSet的文档,以便实现TDataSetDocument
unit DataSetDocument;
interface
uses
Document,
Data.DB;
type
TDataSetDocument = class( TDocument )
private
FDataSet : TDataSet;
protected
function GetValue( const Name : string ) : Variant; override;
public
constructor Create( ADataSet : TDataSet );
function Eof : Boolean; override;
procedure Next; override;
procedure First; override;
end;
implementation
{ TDataSetDocument }
constructor TDataSetDocument.Create( ADataSet : TDataSet );
begin
inherited Create;
FDataSet := ADataSet;
end;
function TDataSetDocument.Eof : Boolean;
begin
Result := FDataSet.Eof;
end;
procedure TDataSetDocument.First;
begin
inherited;
FDataSet.First;
end;
function TDataSetDocument.GetValue( const Name : string ) : Variant;
begin
Result := FDataSet.FieldByName( Name ).Value;
end;
procedure TDataSetDocument.Next;
begin
inherited;
FDataSet.Next;
end;
end.
并使用它
var
LDoc : TDocument;
LDoc := TDataSetDocument.Create( MyQuery );
try
LDoc.ExportStrategy := TXliffPlainExporter.Create;
LDoc.ExportTo( 'test' );
finally
LDoc.Free;
end;