Sunday 18 January 2009

Read an XML document with namespaces and import the data by path using DELPHI.

const
NSMY = 'http://my.namspace.com/namespace';
PATH = '/my:myFields/my:myFields_1/my:FieldName_1';

var
text: string;
Doc: DOMDocument;
node:IXMLDOMNode;

begin
Doc:=CreateComObject(CLASS_DOMDocument) AS IXMLDOMDocument2;
Doc.load('c:\testXML\testForm.xml');
Doc.setProperty('SelectionNamespaces', Format('xmlns:my="%s"', [NSMY]));
node := Doc.selectSingleNode(PATH);
Text:=node.Text;
end;

To access a node in the 'my:' namespace by xpath use the units MSXML2_TLB and ComObj.
You can create MSXML2_TLB by importing the type library for MSXMLv3.0.