3

I have been trying to work with Delphi 2010 and MSXML for the past few days, I am an extreme novice and need a little direction.

var
    MemoryStream: TMemoryStream;
    XMLPath: String;
    sName: String;
    XMLDoc: variant;
    doc: TStringList;
begin
  //unrelated code
  // Create XML File to hard disk
    begin
        MemoryStream := TMemoryStream.Create;

        IdHTTP1.get('http://somewebsite' + , MemoryStream);
        MemoryStream.Position := 0;
        MemoryStream.SaveToFile('data.xml');
        MemoryStream.Free;

    end;
    // Load XML file for data display

    doc:=TStringList.Create;
    doc.LoadFromFile('data.xml');

    XMLDoc := CreateOleObject('Msxml2.DOMDocument.6.0');
    XMLDoc.async := false;
    XMLDoc.LoadXML(doc.Text);

As you can see I am able to load the data into an XML file on harddisk, I then load that file into a DomDocument. I'm stuck from this point on...I want to use this data as I would a recordset in ADO (ex. SomeVariable := rs.Fields.Item('DesiredData').Value). I have done some research and read several methods. However I am unable to figure this one out. I know its got to be something trivial, I'm just not far enough along yet to understand it.

There seem to be plenty of good examples on how to write to an XML file but none on howto use the data.

James West
  • 771
  • 1
  • 14
  • 26
  • So, is it fair to say that your question isn't so much about how to use MSXML as it is about how to apply a database-like interface to an XML document? The title question is misleadingly vague. – Rob Kennedy Aug 27 '10 at 01:07
  • 1
    Unless you have a specific reason not to, it is easier to use the TXMLDocument wrapper provided with Delphi instead of MSXML directly. Here is the TXMLDocument reference page: http://docwiki.embarcadero.com/VCL/en/XMLDoc.TXMLDocument – Alan Clark Aug 27 '10 at 01:43
  • I will try the TXMLDocument method again. I had tried to use it before but was getting some weird Access Violations...I'll modify the title for clarity. – James West Aug 27 '10 at 01:53

2 Answers2

5

I think you could do somethig with this in the next lines:

someNode := XMLDoc.selectSingleNode('//route/to/node');
str := someNode.text;

The parameter for selectSingleNode is basicaly an XPath expression, so you can query attribute nodes like: //route/to/node/@attrib

Here is the MSDN reference to selectSingleNode: http://msdn.microsoft.com/en-us/library/ms757846(v=VS.85).aspx and here is the XPath sintax: http://msdn.microsoft.com/en-us/library/ms256471(v=VS.85).aspx

Also, I can point you to a good XML library for XML Manipulation from Delphi that is also compatible with MSXML but you don't have to use variants directly : http://www.omnixml.com/

And a much better approach if your XML doesn't change much, is to use the XML Data Binding wizard, that basically creates a complete object model from a XML or XSD (it makes to create or read an XML as easy as instanciate a composite object, creating the classes and methods you need): http://www.youtube.com/watch?v=4D78MG4CaAI&feature=player_embedded

SalvadorGomez
  • 552
  • 4
  • 15
  • someNode could be a variant too, of course. actually is an object implementing the `IXMLDOMNode` interface, and selectSingleNode returns a null if the node is not found. – SalvadorGomez Aug 27 '10 at 03:19
  • Thanks Salvador. This is very helpful, I will be researching this info. I had attempted to use the XML Data Binding in Delphi but hadn't been able to get what I was needing. I will try working with that some more if the SelectSingleNode doesn't work out. – James West Aug 27 '10 at 15:58
  • After more work the data binding wizard was the solution I needed – James West Oct 08 '10 at 13:43
1

If you would like to use you XML like a data container (database like) the SimpleStorage is probably what you are looking for. You can find it here:

http://www.cromis.net/blog/downloads/simplestorage/

It uses OmniXML as an XML parser. With SimpleStorage it is very easy to query and manipulate data inside XML.

Runner
  • 6,073
  • 26
  • 38