由数据库数据生成XML的方法(有源码)[6]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 oklemon 的 blog

                        <area>912047</area> 
                        <population>19700000</population> 
                </records> 
    </country> 
  
  插入数据
  
  你已经将country表中存在的数据生成了xml文件。因此在这个xml文件中的数据就与country表中是一样的。如果你想将xml文件中的数据插入进country表中又不想删除原来存在的数据的话,将会有主键冲突的错误出现。因此必须先将country表中已经存在的数据删除掉。
  添加另一个按钮和一个memo构件于主窗体。在button2的onclick事件中添加如下代码.memo用来显示数据插入中的状态(成功/失败)。
  procedure tform1.button2click(sender: tobject); 
  var 
     i,ret_val,count:integer; 
     strdata:string; 
  begin 
      //before inserting data in to the country table,make sure that the data in 
      //the generated xml file(country.xml) and country table(dbdemos) are 
      //different. 
      try 
        count:=1; 
        datalist:=tstringlist.create; 
        memo1.clear; 
        doc := createoleobject('microsoft.xmldom') as ixmldomdocument; 
         //load country.xml file 
        doc.load('country.xml'); 
        nlist:=doc.getelementsbytagname('records'); 
        memo1.lines.append('table name  :country'); 
        memo1.lines.append('---------------------'); 
        for i:=0 to nlist.get_length-1 do 
        begin 
           travelchildren(nlist.get_item(i).get_childnodes); 
           //removes the first character(,) from datarecord 
           strdata:=copy(datarecord,2,length(datarecord)); 
           memo1.lines.append(strdata); 
           datarecord:=''; 
           ret_val:=insertintotable(datalist); 
           if ret_val=1 then 
               memo1.lines.append('data inserted successfully.............!') 
           else if ret_val=-1 then 
               memo1.lines.append('error while updating.....try again.....!'); 
           memo1.lines.append('=============================================' 
                              +'==(record no. :'+inttostr(count)+')'); 
           datalist.clear; 
           count:=count+1; 
        end; 
      except 
        on e:exception do 
           showmessage(e.message); 
     end; 
  end; 
  
  nlist(refer above program) contains a list of nodes.in our case the first node list is... 
  

本文关键:xml delphi
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top