3

我在我的 xml 文件中使用了 delphi 数据绑定向导,一切都编译并运行良好。

我的表单上有 3 个组合框。制造商、型号和年份。

使用 FormCreate 上的以下代码填充制造商。

procedure TfrmMain.FormCreate(Sender: TObject);
var
  RGearing : IXMLracegearingType;
  i : Integer;
begin
  // Load XML Document into Memory
  RGearing := Getracegearing(XMLDocument1);

  // Populate Manufacturer combobox
  for i := 0 to RGearing.Car.Count-1 do
  begin
    cbManufac.Items.Add(RGearing.Car[i].Manufacturer);
  end;

  // Copy current selected Manufacturer to string variable
  varManufac := cbManufac.ListItems[(cbManufac.ItemIndex)].Text;
end;

我的问题是如何根据所选的当前制造商填充模型组合框。

这是随附的 XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<gearing>
  <car>
    <id>1</id>
    <manufacturer>Ford</manufacturer>
    <model>Test 1</model>
    <year></year>
  </car>
  <car>
    <id>2</id>
    <manufacturer>Ford</manufacturer>
    <model>Test 2</model>
    <year></year>
  </car>
  <car>
    <id>3</id>
    <manufacturer>Honda</manufacturer>
    <model>Test 1</model>
    <year></year>   
  </car>
  <settings>
    <form_height></form_height>
    <form_width></form_width>
  </settings>
</gearing>

因此,如果选择的制造商是福特,那么模型组合框需要显示测试 1 和测试 2 作为项目。

4

2 回答 2

5

可以用XPath,试试这样的一句话./gearing/car[manufacturer="Ford"]/model

这是一个基本示例

{$APPTYPE CONSOLE}

uses
  ActiveX,
  Variants,
  ComObj,
  Classes,
  SysUtils;

const
 XMLStr =
 '<?xml version="1.0" encoding="UTF-8"?>'+
 '<gearing>'+
 ' <car>'+
 '   <id>1</id>'+
 '   <manufacturer>Ford</manufacturer>'+
 '   <model>Test 1</model>'+
 '   <year></year>'+
 ' </car>'+
 ' <car>'+
 '   <id>2</id>'+
 '   <manufacturer>Ford</manufacturer>'+
 '   <model>Test 2</model>'+
 '   <year></year>'+
 ' </car>'+
 ' <car>'+
 '   <id>3</id>'+
 '   <manufacturer>Honda</manufacturer>'+
 '   <model>Test 1</model>'+
 '   <year></year>'+
 ' </car>'+
 ' <settings>'+
 '   <form_height></form_height>'+
 '   <form_width></form_width>'+
 ' </settings>'+
'</gearing>';



function GetModels(const manufacturer:string):TStringList;
const
  Msxml2_DOMDocument='Msxml2.DOMDocument.6.0';
var
  XmlDoc         : OleVariant;
  Nodes          : OleVariant;
  lNodes         : Integer;
  i              : Integer;
begin
  Result:=TStringList.Create;
  //create an instance to the XML DOM
  XmlDoc       := CreateOleObject(Msxml2_DOMDocument);
  try
    XmlDoc.Async := False;
    XmlDoc.LoadXML(XMLStr);
    XmlDoc.SetProperty('SelectionLanguage','XPath');
    //check for errors in the xml file
      if (XmlDoc.parseError.errorCode <> 0) then
       raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]);

    //select the nodes with match with the expression
    //sample ./gearing/car[manufacturer="Ford"]/model
    Nodes := XmlDoc.selectNodes(Format('./gearing/car[manufacturer="%s"]/model',[manufacturer]));
    //get the number of nodes selected
    lNodes:= Nodes.Length;
    //traverse the nodes
     for i:=0 to lNodes- 1 do
      Result.Add(Nodes.Item(i).Text);

  finally
   XmlDoc :=Unassigned;
  end;
end;

Var
 Models : TStringList;
begin
 try
    CoInitialize(nil);
    try
      Models:=GetModels('Ford');
      try
        Writeln(Models.Text);
      finally
       Models.Free;
      end;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
于 2012-10-01T19:41:19.073 回答
0

我看不到你的功能和类型

RGearing : IXMLracegearingType; 
RGearing :=Getracegearing(XMLDocument1);

所以我只能用下面的代码来测试它。
浏览代码并根据您的需要对其进行自定义。

在此处输入图像描述

unit xmlCombo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    cbManufac: TComboBox;
    cbModel: TComboBox;
    procedure getManufac;
    procedure getModel(const ManufacVal:string);
    procedure FormActivate(Sender: TObject);
    procedure cbManufacClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses MSXML,ActiveX,ComObj;

const
 XMLTestStr =
 '<?xml version="1.0" encoding="UTF-8"?>'+
 '<gearing>'+
 ' <car>'+
 '   <id>1</id>'+
 '   <manufacturer>Ford</manufacturer>'+
 '   <model>Test 1</model>'+
 '   <year></year>'+
 ' </car>'+
 ' <car>'+
 '   <id>2</id>'+
 '   <manufacturer>Ford</manufacturer>'+
 '   <model>Test 2</model>'+
 '   <year></year>'+
 ' </car>'+
 ' <car>'+
 '   <id>3</id>'+
 '   <manufacturer>Honda</manufacturer>'+
 '   <model>Test 1</model>'+
 '   <year></year>'+
 ' </car>'+
 ' <settings>'+
 '   <form_height></form_height>'+
 '   <form_width></form_width>'+
 ' </settings>'+
'</gearing>';

var
  varManufac : string;
  RGearing  : IXMLDOMDocument;

procedure TForm1.cbManufacClick(Sender: TObject);
begin
     getModel(cbManufac.Items[cbManufac.ItemIndex]);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  RGearing:=CoDOMDocument.Create;
  RGearing.loadXML(XmlTestStr);
  getManufac;
end;

procedure TForm1.getModel(const ManufacVal:string);
Var
  XMLDOMNodeL  : IXMLDOMNodeList;
  ChildST : String;
  ChildN,BNode : IXMLDOMNode;
  i : Integer;

begin
  cbModel.Items.Clear;
  cbModel.Text:='';
  XMLDOMNodeL:=RGearing.getElementsByTagName('car');
  for i := 0 to XMLDOMNodeL.length-1 do
  begin
    ChildN:=XMLDOMNodeL[i].selectSingleNode('manufacturer');
    if ChildN.text=ManufacVal then begin
       BNode:=XMLDOMNodeL[i].selectSingleNode('model');           
       ChildST:=BNode.text;
       if cbModel.Items.IndexOf(ChildST) = -1 then cbModel.Items.Add(ChildST);
    end;
  end;
end;

procedure TForm1.getManufac;
Var
  XMLDOMNodeList  : IXMLDOMNodeList;
  ChildST : string;
  i : Integer;

begin
  cbManufac.Items.Clear;
  cbManufac.Text:='';
  // Populate Manufacturer combobox
  XMLDOMNodeList:=RGearing.getElementsByTagName('manufacturer');
  for i := 0 to XMLDOMNodeList.length-1 do
  begin
    ChildST:=XMLDOMNodeList[i].text;
    if cbManufac.Items.IndexOf(ChildST) = -1 then cbManufac.Items.Add(ChildST);
    if cbManufac.Items.Count = 1 then begin
       cbManufac.Text:=ChildST;
       getModel(ChildST);
    end;
  end;
end;

end.

从文件加载 xml。

if RGearing.Load('File.xml') then
  [...]
 else
  ShowMessage('Could not load file : File.xml');
end;

我希望这有帮助。

于 2012-10-02T01:57:01.897 回答