Sunday 7 February 2010

MSXML - enable xpath query

To enable MSXML for use with XPATH queries you have to set the name spaces and also the language property to XPath like so:-

myDoc := CreateComObject(CLASS_msDOMDocument) AS IXMLDOMDocument2;
myDoc.setProperty('SelectionNamespaces', Format('xmlns:my="%s"', [NSMY]));
myDoc.setProperty('SelectionLanguage','XPath'); // enable advanced query

Sunday 10 January 2010

Delphi: Read XML document from an embedded resource.

If your application wants to work with XML data; sometimes it is useful to shove that data into the executable; so it can start with a 'blank form' for example.

You can pack XML into a resource in Delphi and read it using a TResourceStream like this:-


procedure TForm1.loadInternally(var myDoc: msDOMDocument);

const
NSMY = 'http://schemas/mynamespace/blah/blah';

var
resource: TResourceStream;
xmlString: string;
l: integer;
sstream: TStringStream;

begin

myDoc := CreateComObject(CLASS_msDOMDocument) AS IXMLDOMDocument2;
myDoc.setProperty('SelectionNamespaces', Format('xmlns:my="%s"', [NSMY]));

// get embedded data
resource :=TResourceStream.CreateFromID(HInstance, 500, RT_RCDATA);
sstream :=TStringStream.Create;
resource.SaveToStream( sstream );
xmlString:= sstream.DataString;
// and load..
if not myDoc.loadXML(xmlString) then
showmessage('Internal Document load failed');

// release memory.

resource.Free;
sstream.Free;

end;