Libraries and tutorials for XML in Delphi

I'm planning to add XML support to application, but I'm not familiar with XML programming in Delphi. Basically I need to create objects based on XML nodes and generate XML file based on objects.

Which XML component library I should use? Are there any good tutorials for XML with Delphi?


Solution 1:

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.

Solution 2:

I use nativeXML from simdesign. It takes all the pain out of working with XML you will be up and running in minutes.

Solution 3:

Here are a couple of tutorials:

  • Creating, Parsing and Manipulating XML Documents with Delphi
  • Basic XML Parsing in Delphi

Additionally, you may want to look into the XMLIntf unit (although this linked Delphi Wikia page is very light on content).