Replacing TXMLDocument (DOM-based) XML-generation with a solution that is able to write to a stream while the XML file is generated

风流意气都作罢 提交于 2019-12-20 03:17:14

问题


I have a whole lot of code which generates a XML using TXMLDocument, that looks like this:

function Obj.SaveToXmlNode(XmlNode: IXmlNode; SubnodeName: string): IXmlNode;
begin
    Result := XmlNode.AddChild(SubnodeName);
    SaveFieldToXmlNode(Result, 'FIELD1', 'Value1', 'PrevValue1');
    SaveFieldToXmlNode(Result, 'FIELD2', 'Value2', 'PrevValue2');
    //...
end;

with

function SaveFieldToXmlNode(XmlNode: IXmlNode; FieldName: string; NewVal: Variant;
            OldVal: Variant): IXmlNode;
var
    FieldNode: IXMLNode;
begin
    FieldNode := XmlNode.AddChild(FieldName);
    FieldNode.NodeValue := NewVal;
    if not VarIsEmpty(OldVal) then
            FieldNode.Attributes[XML_OLDVALUE] := OldVal;
end;

I have run into serious issues using this implementation for generating a big XML file:

  1. Memory usage: for big files I ended up using more than a GB of memory like similar to this case.
  2. The generation of the XML file is not very fast.
  3. My main problem: The XML is generated inside a webserver as an answer to a http request. To avoid problems, the XML must be written to the stream while it is being generated.

I attributed the problems to using a DOM-based XML-writer, but perhaps even a DOM-based writer can handle this situation by partially writing (and locking) generated content into a stream.

How can I resolve this situation without touching my existing code too much?

Edit: While this question was closed, I posted another related but more common question, which targets the general ability of Delphi to directly write XML.


回答1:


You might want to have a look at NativeXML or OmniXML



来源:https://stackoverflow.com/questions/18654127/replacing-txmldocument-dom-based-xml-generation-with-a-solution-that-is-able-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!