如何从xml文件中读取数据并以delphi语言显示在文本框中

时间:2011-11-11 06:51:01

标签: delphi delphi-2009

我是delphi语言的新手,在这里我有一个疑问,我有一个名为vehicle.xml的xml文件。

看起来像这样

<data>
<vehicle>
    <type>Car</type>
    <model>2005</model>
    <number>1568</number>
</vehicle>
<vehicle>
    <type>Car</type>
    <model>2009</model>
    <number>1598</number>
</vehicle>
</data>

我的Delphi表单包含三个文本框:

  • txtType
  • txtModel
  • txtnumber

在加载页面时,我想在文本框中显示vehicle.xml的内容,如:

  • txtType =车载
  • txtModel = 2005
  • txtNumber = “1568”

3 个答案:

答案 0 :(得分:11)

查看Delphi自己的TXMLDocument组件,例如:

procedure TForm1.FormCreate(Sender: TObject);
var
  Vehicle: IXMLNode;
begin
  XMLDocument1.FileName :='vehicle.xml';
  XMLDocument1.Active := True;
  try
    Vehicle := XMLDocument.DocumentElement;
    txtType.Text := Vehicle.ChildNodes['type'].Text;
    txtModel.Text := Vehicle.ChildNodes['model'].Text;
    txtnumber.Text  := Vehicle.ChildNodes['number'].Text;
  finally
    XMLDocument1.Active := False;
  end;
end;

或者,直接使用IXMLDocument接口(TXMLDocument包装):

procedure TForm1.FormCreate(Sender: TObject);
var
  Doc: IXMLDocument;
  Vehicle: IXMLNode;
begin
  Doc := LoadXMLDocument('vehicle.xml');
  Vehicle := Doc.DocumentElement;
  txtType.Text := Vehicle.ChildNodes['type'].Text;
  txtModel.Text := Vehicle.ChildNodes['model'].Text;
  txtnumber.Text := Vehicle.ChildNodes['number'].Text;
end;

更新:问题中的XML已更改为现在将vehicle元素包含在data元素内,并且具有多个vehicle元素。所以上面的代码必须相应调整,例如:

procedure TForm1.FormCreate(Sender: TObject);
var
  Doc: IXMLDocument;
  Data: IXMLNode;
  Node: IXMLNode;
  I: Integer;
begin
  Doc := LoadXMLDocument('vehicle.xml');
  Data := Doc.DocumentElement;
  for I := 0 to Data.ChildNodes.Count-1 do
  begin
    Node := Data.ChildNodes[I];
    // if all of the child nodes will always be 'vehicle' only
    // then this check can be removed...
    if Node.LocalName = 'vehicle' then
    begin
      // use Node.ChildNodes['type'], Node.ChildNodes['model'],
      // and Node.ChildNodes['number'] as needed...
    end;
  end;
end;

答案 1 :(得分:4)

您可以使用单位MSXML(或任何其他XML解析器)读取XML文件。

它为您提供表示XML文件的树结构。其中车辆是顶部节点而其他三个是子节点。

每个节点都有一个可用于获取值的text属性。您可以将其分配给表单上的文本框。

代码示例:

uses
  ActiveX,
  MSXML;

procedure TForm1.ReadFromXML(const AFilename: string);
var
  doc : IXMLDOMDocument;
  node : IXMLDomNode;

begin
  CoInitialize; // Needs to be called once before using CoDOMDocument.Create;
  if not FileExists(AFileName) then 
    Exit; // Add proper Error handling.

  doc := CoDOMDocument.Create;
  doc.load(AFileName);

  if (doc.documentElement = nil) or (doc.documentElement.nodeName <> 'vehicle') then
    Exit; // Add proper Error handling.

  node := doc.documentElement.firstChild;
  while node<>nil do begin
    if node.nodeName = 'model' then
      txtModel.Text := node.text;
    if node.nodeName = 'number' then
      txtNumber.Text := node.text;
    if node.nodeName = 'type' then
      txtType.Text := node.text;
    node := node.nextSibling;
  end;
end;

答案 2 :(得分:-1)

smses count =“ 1” backup_set =“ b8ea1116-9614-41d0-ac5b-ef93c27089cd” backup_date =“ 1605468953370” type =“ full”>