在Delphi中生成XML时如何编写XML?

时间:2013-09-09 08:40:14

标签: xml delphi xml-serialization

我在Delphi中使用TXMLDocument生成XML文件。在构建XML之后,我调用SaveToStream。因为构建XML文件需要很长时间,所以我需要一个解决方案来在流生成时写入流。怎么办?

1 个答案:

答案 0 :(得分:1)

TXMLDocument无法实现。你必须找到另一个框架 做你需要的,或者只是手动写入流,例如:

procedure WriteToStream(Stream: TStream; const S: UTF8String);
begin
  Stream.WriteBuffer(Pointer(S)^, Length(S));
end;

var
  Stream: TStream;
begin
  Stream := ...;
  try
    WriteToStream(Stream, '<?xml version="1.0" encoding="utf-8" ?>'#13);
    WriteToStream(Stream, '<my_doc_element>');
    WriteToStream(Stream, '<my_child_element>');
    ...
    WriteToStream(Stream, '</my_child_element>');
    WriteToStream(Stream, '</my_doc_element>');
  finally
    Strm.Free;
  end;
end;