灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:9377回复:1

delphi读取xml文件

楼主#
更多 发布于:2012-09-08 13:06

在使用delphi的时候不免要与配置文件打交道。那么xml无疑是配置文件的最好格式,那么怎么操作xml呢?其实很简单,看看下面的代码吧。
[delphi]
  功能:  根据省份更新地市信息
-------------------------------------------------------------------------------}
procedure TForm.GetCityInfo;
var
  iXMLAreaFile : IXMLDocument;
  sFileName,aProvince,sProvince,sCity : string;
  nCol,nNum,nCityCount,nprovinceCount : Integer;
begin
  iXMLAreaFile := TXMLDocument.create(nil);
  sFileName :=  getexepath + 'Province.xml';
  iXMLAreaFile.FileName := sFileName;
  iXMLAreaFile.active := True;
  sProvince := cbbprovince.Text;
  if sProvince = '全部' then
  begin
    cbbcity.Items.Clear;
    cbbcity.Items.Add('全部');
    cbbcity.ItemIndex := 0;
  end
  else
  begin
    cbbcity.Items.Clear;
    cbbcity.Items.Add('全部');
    cbbcity.ItemIndex := 0;
    nprovinceCount := iXMLAreaFile.DocumentElement.childnodes.Count;
    for nNum := 0 to nprovinceCount-1 do
     begin
       //获取province节点内容 ww.atcpu.com  
       aProvince := iXMLAreaFile.DocumentElement.ChildNodes[nNum].
       AttributeNodes[0].nodevalue;
       if aProvince = sProvince then
       begin
         ncityCount := iXMLAreaFile.DocumentElement.childnodes[nNum].childnodes.Count;
         //获取市级节点内容
         for nCol := 0 to ncitycount-1 do
         begin
           sCity := iXMLAreaFile.DocumentElement.ChildNodes[nNum].
           childnodes[nCol].AttributeNodes[0].nodevalue;
           cbbCity.Items.Add(sCity);
         end;
       end;
     end;
  end;
end;


简单吧。先定义XML文件然后读取信息。







摘自 苏生-苏米沿的专栏




喜欢0 评分0
Yuki520
禁止发言
禁止发言
  • 注册日期2022-07-04
  • 发帖数4
  • QQ2663812151
  • 火币5枚
  • 粉丝0
  • 关注0
沙发#
发布于:2022-07-04 10:48
用户被禁言,该主题自动屏蔽!

回复(0) 喜欢(0)     评分
游客

返回顶部