访问webservice,导入xml至本地数据集

  1. 创建本地数据集

     clientdataset2.FieldDefs.Clear;;
      with clientdataset2.FieldDefs.AddFieldDef do
      begin
        Name := 'id';
        Size := 10;
        DataType := ftString;
      end;
      with clientdataset2.FieldDefs.AddFieldDef do
      begin
        Name := 'xm';
        size := 10;
        DataType := ftstring;
      end;
      with clientdataset2.FieldDefs.AddFieldDef do
      begin
        Name := 'xb';
        Size := 10;
        DataType := ftString;
      end;
      with clientdataset2.FieldDefs.AddFieldDef do
      begin
        Name := 'nl';
        DataType := ftinteger;
      end;
      with clientdataset2.FieldDefs.AddFieldDef do
      begin
        Name := 'sfzh';
        Size := 10;
        DataType := ftString;
      end;
      with clientdataset2.FieldDefs.AddFieldDef do
      begin
        Name := 'jtzz';
        size := 30;
        DataType := ftstring;
      end;
      clientdataset2.CreateDataSet;
      clientdataset2.open;

  2. 导入xml

    var
      xml:Tnativexml;
      root: TsdElement;
      i:integer;
      ////////////声明soapheader
      aIntf: DataCenterServicesSoap;
      Headers: ISOAPHeaders;
      H: SecuritySoapHeader2;//SecuritySoapHeader2;
    begin
      Try
        clientdataset2.EmptyDataSet;
        xml := Tnativexml.Create(nil);

        ////////////////////////////////////////////////////发送soapheader
        aIntf := (httprio1 as DataCenterServicesSoap);

        H := SecuritySoapHeader2.Create;
        h.Ticket := 'BE2E4F35-CDE9-46A1-844B-9FB2546491B2';

        Headers := (aIntf as ISOAPHeaders);
        Headers.Send(H); //发送Soap Header
       // xml.ReadFromString((httprio1 as DataCenterServicesSoap).getdataasxml(memo1.Text));
        //aIntf.GetInfo; //调用服务
        xml.ReadFromString((httprio1 as DataCenterServicesSoap).GetXML(memo1.Text));
        xml.XmlFormat := xfReadable;
        root := xml.Root;
        for i := 0 to root.NodeCount - 1 do
        begin
          clientdataset2.Append;
          clientdataset2.FieldByName('id').Value := root[i].NodeByName('ID').Value;
          clientdataset2.FieldByName('xm').Value := root[i].NodeByName('xm').Value;
          clientdataset2.FieldByName('xb').Value := root[i].NodeByName('xb').Value;
          clientdataset2.FieldByName('nl').Value := root[i].NodeByName('nl').Value;
          clientdataset2.FieldByName('sfzh').Value := root[i].NodeByName('sfzh').Value;
          clientdataset2.FieldByName('jtzz').Value := root[i].NodeByName('jtzz').Value;
          clientdataset2.Post;
        end;
      finally
        aIntf := nil;
      End;

      xml.Free;
      H.Free;

------------------------------------------------------------------

var
  xml:Tnativexml;
  root: TsdElement;
  i:integer;
  aIntf: DataCenterServicesSoap;
  ////////////声明soapheader
begin
  Try
    clientdatasetxz.EmptyDataSet;
    xml := Tnativexml.Create(nil);

    ////////////////////////////////////////////////////发送soapheader
    aIntf := (httprio1 as DataCenterServicesSoap);
   // roota := xml.root.NodeNew('NewDataset');
    xml.ReadFromString((httprio1 as DataCenterServicesSoap).GetXZZD);
    memo6.lines.text := (httprio1 as DataCenterServicesSoap).GetXZZD;
    xml.XmlFormat := xfReadable;
    root := xml.Root;
    for i := 0 to root.NodeCount - 1 do
    begin
        Memo4.Lines.Add(root[i].Name + #9 + root[i].Value); // root[i] = root.Nodes[i]

         Memo4.Lines.Add( root[i].NodeByName('unitId').Value + #9 + root[i].NodeByName('name').Value); // root[i] = root.Nodes[i]
    end;
    {
    for i := 0 to root.NodeCount - 1 do
    begin
     {
      clientdatasetxz.Append;
      clientdatasetxz.FieldByName('unitid').Value := root[i].NodeByName('unitId').Value;
      clientdatasetxz.FieldByName('name').Value := root[i].NodeByName('name').Value;
      clientdatasetxz.Post;
      }
     {
      memo4.lines.add(root.NodeByName('unitId').Value + ' ' +  root.NodeByName('name').Value);
      // memo4.lines.add(root[i].Nodes[0].Value + ' ' +  root[i].Nodes[1].Value);
    end;
    }

  finally
    aIntf := nil;
  End;

  xml.Free;
  //H.Free;

你可能感兴趣的:(xml,webservice,Delphi)