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

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

本文简介:选择自 oklemon 的 blog

          <records> 
                        <name>argentina</name> 
                        <capital>buenos aires</capital> 
                        <continent>south america</continent> 
                        <area>2777815</area> 
                        <population>32300003</population> 
                </records> 
  
  
  我们传送此节点列表给一个递归函数,travelchildren。它将递归地沿着节点列表查找文本数据,并将此数据加入tstringlist(datalist)变量中。当完成第一轮后,datalist中将会包含字符串 argentina,buenos aires,south america,2777815,32300003.最后我们将此stringlist传送给函数 insertintotable,它将完成将一条记录插入 country 表的工作。重复此过程即可完成整个xml文件数据的插入工作。
  procedure tform1.travelchildren(nlist1:ixmldomnodelist); 
  var 
     j:integer; 
     temp:string; 
  begin 
    for j:=0 to nlist1.get_length-1 do 
    begin 
    //node type 1 means an entity and node type 5 means entityref 
    if((nlist1.get_item(j).get_nodetype= 1) or (nlist1.get_item(j).get_nodetype=5)) then 
      travelchildren(nlist1.get_item(j).get_childnodes) 
      //node type 3 means a text node,ie you find the data 
      else if(nlist1.get_item(j).get_nodetype=3) then 
      begin 
        temp:= trim(nlist1.get_item(j).get_nodevalue); 
        datarecord:=datarecord+','+temp; //this is for displaying a single record on the memo 
        datalist.add(temp); //datalist will contain one record after completing one full travel through the node list 
      end 
    end; 
  end; 
  
  function tform1.insertintotable(stpt:tstringlist):integer; 
  var 
    i:integer; 
  begin 
    table1.close; 
    table1.open; 
    table1.insert; 
    for i := 0 to stpt.count - 1 do 
    begin 
         table1.fields[i].asvariant:= stpt[i]; 
    end; 
    try 
      table1.post; 
      result:=1; 
    except 
      on e:exception do 
        result:=-1; 
    end; 
  end; 
  
  结论:

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

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

go top