Working with XML in Delphi (returning specific dat

2019-04-12 07:56发布

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.

2条回答
Bombasti
2楼-- · 2019-04-12 08:19

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-04-12 08:30

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

查看更多
登录 后发表回答