xmldocument的使用

本文介绍了一种使用 Delphi 从 XML 数据源中解析国家单位数据的方法。通过创建 TXMLDocument 对象并加载 XML 字符串,可以遍历 XML 结构来提取每个单位的 ID 和名称,并将这些信息存入客户端数据集。最后,将数据集中的单位名称填充到下拉列表框中。

TXMLDocument domvendor = ADOM XML v4

var
  root: IXMLnode;
  lei: IXMLNode;
  info: IXMLNode;
  i: integer;
  strxml:string;
  xmldoc:TXMLDocument;
begin
  cbbCountry.Items.Clear;
  try
    strxml := '<?xml version="1.0" encoding="GB2312"?>';
    strxml := strxml + (httprio1 as DataCenterServicesSoap).GetCZD(xz);
    xmldoc :=  TXMLDocument.create(nil);
    xmldoc.LoadFromXML(strxml);
    root := xmldoc.DocumentElement;
    lei := root.ChildNodes.First;
    while lei <> nil do
    begin
      if lei.NodeName = 'Item' then
      begin
        info := lei.ChildNodes.First;
        clientdatasetcountry.append;
        while info <> nil do
        begin
          if info.NodeName = 'unitId' then
          begin
            clientdatasetcountry.fieldbyname('unitId').value := info.text;
          end
          else if info.NodeName = 'name' then
          begin
            clientdatasetcountry.fieldbyname('name').value := info.text;
          end;
          info := info.NextSibling;
        end;
      end;
      clientdatasetcountry.post;
      lei := lei.NextSibling;
    end;
 finally
   xmldoc.free;
 end;
  cbbcountry.items.Clear;
  clientdatasetcountry.first;
  while not clientdatasetcountry.eof do
  begin
    if  trim(clientdatasetcountry.FieldByName('name').asstring) <> '' then
      cbbcountry.items.add( clientdatasetcountry.FieldByName('name').asstring);
    clientdatasetcountry.next;
  end;
















本文转自鹅倌51CTO博客,原文链接:http://blog.51cto.com/kaixinbuliao/1528423 ,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值