Libraries and tutorials for XML in Delphi

You can start by looking at Delphi’s TXMLDocument component. This will provide you with the basics of working with XML/DOM. It’s simple and can be added by dropping it onto your Form. It has LoadFromFile and SaveToFile methods and is easily navigated.

However, at some point you will exhaust TXMLDocument’s features, especially if you want to work with things like XPath.

I suggest you look at IXMLDOMDocument2 which is part of MSXML2_TLB, e.g.

  XML := CreateOleObject('MSXML2.DOMDocument.3.0') as IXMLDOMDocument2;
  XML.async := false;
  XML.SetProperty('SelectionLanguage','XPath');

You will need to add msxmldom, xmldom, XMLIntf, XMLDoc & MSXML2_TLB to your uses section.

There are a few component libraries out there but I would suggest writing your own helper class or functions. Here’s an example of one we wrote and use:

function XMLCreateRoot(var xml: IXMLDOMDocument2; RootName: string; xsl: string = ''; encoding: string = 'ISO-8859-1'; language: string = 'XPath'): IXMLDOMNode;
var
  NewPI:   IXMLDOMProcessingInstruction;
begin

  if language<>'' then
     xml.SetProperty('SelectionLanguage','XPath');

  if encoding<>'' then begin
     NewPI:=xml.createProcessingInstruction('xml', 'version="1.0" encoding="'+encoding+'"');
     xml.appendChild(NewPI);
  end;

  if xsl<>'' then begin
     NewPI:=xml.createProcessingInstruction('xml-stylesheet','type="text/xsl" href="'+xsl+'"');
     xml.appendChild(NewPI)
  end;

  xml.async := false;
  xml.documentElement:=xml.createElement(RootName);
  Result:=xml.documentElement;
end;

Take it from there.

Leave a Comment