2

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.

Community
  • 1
  • 1
Alois Heimer
  • 1,772
  • 1
  • 18
  • 40
  • The plain XML format is simple and textual. I was able to write XML-exporting routines on an "empty" afternoon, so if you want to do something like that it is not too complex, however some functionality of the DOM (like pretty-printing a huge XML file) can be a bit trickier to implement. – mg30rg Sep 06 '13 at 10:51
  • 1
    I agree, it is not _that_ much work, but there are tricky things, like handling of special chars, indentation and so on. I have used XmlSerializer in Java and thought, there should be something comparable in Delphi. And I think I am not the first one who switches from a DOM to directer XML creation, so perhaps there already exists a comfortable solution. – Alois Heimer Sep 06 '13 at 11:15
  • Since you have your code based on interfaces, what you need is new implemetor classes for those very same interfaces (a replacement for TXMLDocument). So, your code would not change at all. – AlexSC Sep 06 '13 at 12:53
  • I have reformulated the question to hopefully meet the guidelines. If the question is OK now, I would kindly ask to vote to reopen it. If you think it is not OK, please leave a comment, that I can workout the issue. – Alois Heimer Sep 06 '13 at 14:08
  • @DavidHeffernan I have further edited the question, to avoid the closure of the question because of asking for a "recommendation to find a library" (as you requested). Could you please leave me a comment, if you think, it is OK now. – Alois Heimer Sep 09 '13 at 11:59
  • @AndrewBarber You marked this question as off-topic because it would not "demonstrate a minimal understanding of the problem". Could you please explain, what is not understandable, so that I can fix it? – Alois Heimer Sep 11 '13 at 07:36
  • that was a mistake. thought I was closing something else, I assume. fixed! – Andrew Barber Sep 11 '13 at 10:45

1 Answers1

0

You might want to have a look at NativeXML or OmniXML

iamjoosy
  • 3,299
  • 20
  • 30
  • Aren't they DOM based? – David Heffernan Sep 06 '13 at 12:01
  • @DavidHeffernan OmniXML is DOM-based. I thought NativeXML was DOM-based too, but I could not find any evidence. I'll have a closer look at it. Nevertheless, for [my problem](http://stackoverflow.com/q/18601630/2523663) I need a solution that is able to write to the stream, while the XML is being generated. I haven't found how this could be achieved by NativeXML. – Alois Heimer Sep 06 '13 at 12:38