tags:

views:

2807

answers:

6

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?

+5  A: 

You could try the following book : Delphi Developers Guide to XML

Basically I would recommend you use Microsoft's DOM. You'll need to import the library as with any other COM object.

Steve
Is there any particular reason to use Microsoft's DOM?
Harriv
I find it pretty straight forward to understand. SAX is the other option (not the only other option may I add).
Steve
+3  A: 

You can use Delphi's XML Data Binding (File - New - Other - XML Mapping (I don't know path exactly, I'm at home without Delphi)).

It creates objects/interfaces over XML provider so you can work with objects/structures instead of plain xml text file.

You don't have to make hard work by reading and writing each XML Element - you're just working with collections of objects and theirs properties.

DiGi
This makes an xtr XML transformation file, that you need to include with your exe. It does make the XML a dataset which is easier but less flexible (it only works well with simple XML)
Osama ALASSIRY
+5  A: 

Here are a couple of tutorials:

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

Mattias Andersson
+11  A: 

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.

Gerard
+5  A: 

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

davehay
+2  A: 

I have been working with nativeXML for about a year now. My needs are fairly simple. XML fluency is a small part of a larger application for me, but I have been able to implement the pieces I need almost as fast as I can code them, the online help is good and my needs were met in a day rather than a week or longer. I second davehay's vote for nativeXML.

I'll have to check the nativelXML, sounds good. Are there any downsides?
Harriv