当前位置:编程学习 > XML/UML >>

delphi读取xml文件

在使用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节点内容 www.zzzyk.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文件然后读取信息。

 


摘自 苏生-苏米沿的专栏

补充:软件开发 , Delphi ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,