在Delphi中使用XML(将特定数据返回到变量) [英] Working with XML in Delphi (returning specific data to a variable)

查看:281
本文介绍了在Delphi中使用XML(将特定数据返回到变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去几天一直在尝试使用Delphi 2010和MSXML,我是一个极端的新手,需要一个方向。

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);

如您所见,我可以将数据加载到硬盘上的XML文件中,然后加载该文件进入DomDocument。我从这一点被困住了...我想使用这个数据,就像我在ADO中的记录集(例如SomeVariable:= rs.Fields.Item('DesiredData')。我已经做了一些研究,并阅读了几种方法。但是我无法想出这一个。我知道它必须是微不足道的,我还不够了解,不了解它。

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.

似乎有很多很好的例子,说明如何写一个XML文件,但没有一个如何使用数据。

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

推荐答案

我想你可以在下面的一行做一些事情:

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

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

selectSingleNode的参数是XPath表达式的基础,因此您可以查询属性节点,如: // route / to / node / @ attrib

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

这是MSDN对selectSingleNode的引用: http://msdn.microsoft.com/en-us/library/ms757846(v = VS.85).aspx
这里是XPath sintax: http://msdn.microsoft.com/en-us/library/ms256471(v = VS.85).aspx

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

此外,我可以指出一个很好的XML操作,从Delphi开始,它也兼容MSXML,但你不必直接使用变体: http://www.omnixml.com/

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/

如果您的XML不变化很大,就是使用XML Data Binding向导,基本上创建一个补全来自XML或XSD的te对象模型(它创建或读取一个XML,像instanciate一个复合对象一样简单,创建所需的类和方法): http://www.youtube.com/watch?v=4D78MG4CaAI&feature=player_embedded

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

这篇关于在Delphi中使用XML(将特定数据返回到变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆