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

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

本文简介:选择自 oklemon 的 blog

                  text1,text2 : ixmldomtext; 
                  nlist : ixmldomnodelist; 
                  datarecord : string; 
  
  添加makexml函数到你的单元。它将通过读取dbdemos中contry表中的数据生成一个xml文件。
  function tform1.makexml(table:ttable):integer; 
  var 
    i   : integer; 
    xml,temp : string; 
  begin 
    try 
      table.close; 
      table.open; 
      xml  := table.tablename; 
      doc  := createoleobject('microsoft.xmldom') as ixmldomdocument; 
      //set the root name of the xml file as that of the table name. 
      //in this case "country" 
      root := doc.createelement(xml); 
      doc.appendchild(root); 
      //this while loop will go through the entaire table to generate the xml file 
      while not table.eof do 
      begin 
        //adds the first level children , records 
        child:= doc.createelement('records'); 
        root.appendchild(child); 
        for i:=0 to table.fieldcount-1 do 
        begin 
          //adds second level children 
          child1:=doc.createelement(table.fields[i].fieldname); 
          child.appendchild(child1); 
          //check field types 
          case tfieldtype(ord(table.fields[i].datatype)) of 
          ftstring: 
          begin 
            if table.fields[i].asstring ='' then 
                 temp :='null'  //put a default string 
               else 
                 temp := table.fields[i].asstring; 
          end; 
  
          ftinteger, ftword, ftsmallint: 
          begin 
              if table.fields[i].asinteger > 0 then 
                 temp := inttostr(table.fields[i].asinteger) 
               else 
                 temp := '0'; 
          end; 
          ftfloat, ftcurrency, ftbcd: 
          begin 
              if table.fields[i].asfloat > 0 then 

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

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

go top