views:

103

answers:

2

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.

+3  A: 

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
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
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.
JamesW
After more work the data binding wizard was the solution I needed
JamesW
+1  A: 

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