Friday, 6 September 2013

Generate XML without using DOM in Delphi (replacing existing DOM based implementation)

Generate XML without using DOM in Delphi (replacing existing DOM based
implementation)

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;
For various reasons (speed, memory, stream-writing), I need a XML
generator, that is not DOM-based (like XMLSerializer in Java). Ideally it
should be provide a dropin replacement, so that I would have to modify my
Code only slightly, like this:
function Obj.SaveToXmlNode(XmlNode: IMyXmlNode; SubnodeName: string):
IMyXmlNode;
begin
Result := XmlNode.AddChild(SubnodeName);
SaveFieldToXmlNode(Result, 'FIELD1', 'Value1', 'PrevValue1');
SaveFieldToXmlNode(Result, 'FIELD2', 'Value2', 'PrevValue2');
//...
Result.Flush();
end;
But the dropin is no requirement. Important is, that such a wrapper can
easily be written and that the generator can write directly to a TStream.
Do you have any recommendation?

No comments:

Post a Comment