热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Delphi中访问XML文档的一部分-AccessPartofanXMLDocumentinDelphi

ihaveusedthedelphidatabindingwizardwithmyxmlfile,andeverythingcompilesandrunsfine.

i have used the delphi data binding wizard with my xml file, and everything compiles and runs fine.

我已经使用delphi数据绑定向导和我的xml文件,一切都编译并运行正常。

I have 3 comboboxes on my form. Manufacturer, Model and Year.

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

Manufacturer is populated using the following code on FormCreate.

使用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;

My question is how can i populate the Model combobox based on the current manufacturer that is selected.

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

Here is the XML File that goes with it

这是随附的XML文件



  
    1
    Ford
    Test 1
    
  
  
    2
    Ford
    Test 2
    
  
  
    3
    Honda
    Test 1
       
  
  
    
    
  

So if the manufacturer selected is Ford then the model combobox needs to display Test 1 and Test 2 as the items.

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

2 个解决方案

#1


0  

I can not see your function and the type

我看不到你的功能和类型

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

So I can only test it with the following code.
Go through the code and customize it to your needs.

所以我只能使用以下代码进行测试。浏览代码并根据您的需求进行自定义。

enter image description here

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 =
 ''+
 ''+
 ' '+
 '   1'+
 '   Ford'+
 '   Test 1'+
 '   '+
 ' '+
 ' '+
 '   2'+
 '   Ford'+
 '   Test 2'+
 '   '+
 ' '+
 ' '+
 '   3'+
 '   Honda'+
 '   Test 1'+
 '   '+
 ' '+
 ' '+
 '   '+
 '   '+
 ' '+
'';

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.

To load xml from a file.

从文件加载xml。

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

I hope this helps.

我希望这有帮助。

#2


5  

You can use XPath, try a sentence like this ./gearing/car[manufacturer="Ford"]/model

您可以使用XPath,尝试这样的句子./gearing/car[manufacturer="Ford"]/model

This is a basic sample

这是一个基本的样本

{$APPTYPE CONSOLE}

uses
  ActiveX,
  Variants,
  ComObj,
  Classes,
  SysUtils;

const
 XMLStr =
 ''+
 ''+
 ' '+
 '   1'+
 '   Ford'+
 '   Test 1'+
 '   '+
 ' '+
 ' '+
 '   2'+
 '   Ford'+
 '   Test 2'+
 '   '+
 ' '+
 ' '+
 '   3'+
 '   Honda'+
 '   Test 1'+
 '   '+
 ' '+
 ' '+
 '   '+
 '   '+
 ' '+
'';



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.

推荐阅读
author-avatar
白宇2502858015
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有