delphi7写的个人笔记(模版功能已经实现)介绍三

      这几天都在上班没有把后续的写上,今天休息了,就把这个补上吧奋斗

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

     前面已经说了XML文件的解析,今天我要记录的是XML文件的创建和保存。前面介绍了说我这个笔记本主要的模块的分类:生活资料,学习资料,工作资料,相册记录,而其中的生活资料,学习资料,工作资料,这三个就目前来说,模块的结构和功能都是类似的,主要就是点击按钮,从xml文件中解析出数据结构列表到listview中,同时在listview这个控件所在的窗口实现对数据的增加,删除。解析XML文件就不再多说,这个只能大家在使用过了之后才有自己的体会。今天跟大家说的是XML文件的创建和保存。

在点击add data这个button的时候,会创建一个添加数据的窗口。(先跟大家说一下我这个程序中对XML文件的分类,在程序文件中创建了一个以学习为名字的文件夹,在这个文件夹中创建一个主xml文件,名称为xuexi.xml,这个文件中主要保存的结构为:标题名称,描述和创建日期,同时在xuexi这个文件夹中会有一个以标题命名的XML文件,而这个文件中所存储的结构为标题,内容,和日期,当我们添加一条数据的时候,在xuexi.xml这个文件中会添加一条数据同时在xuexi这个文件下会产生一个以标题命名的XML文件,删除的时候同样会删除这两个位置的数据)。

     那么先看一下增加数据怎么实现!

    

procedure TForm2.btn1Click(Sender: TObject);
var
  iFileHaddle:Integer;
  strDate:string;
  xn,nTitle,nMainInfomation,nDescription,nWriteTime:IXMLNode;
  lNodes:IXMLNodeList;
begin
     strDate:=FormatDateTime('c',Now);
     XMLDocument1.XML.Clear;
     XMLDocument1.XML.Add('<?xml version="1.0" encoding="GBK"?>');
     XMLDocument1.XML.Add('<xuexi>');
     XMLDocument1.XML.Add('<title>'+edt1.Text+'</title>');
     XMLDocument1.XML.Add('<mainInfomation>'+mmoMain.Text+'</mainInfomation>');
     XMLDocument1.XML.Add('<description>'+edt2.Text+'</description>');
     XMLDocument1.XML.Add('<writeTime>'+strDate+'</writeTime>');
     XMLDocument1.XML.Add('</xuexi>');
     XMLDocument1.Active:=True;
     //iFileHaddle:=FileOpen('xuexi\'+edt1.Text+'.xml',fmOpenRead);
     if (Trim(edt1.Text)<>'') and (Trim(edt2.Text)<>'') and  (Trim(mmoMain.Text)<>'') then
     begin
         XMLDocument1.SaveToFile('xuexi\'+edt1.Text+'.xml');
         //FileClose(iFileHaddle);
         ShowMessage('successfully!');
         Form2.Close;

     end
     else
     ShowMessage('please input the information without not null values');

//上面的是在xuexi这个文件夹中创建以title命名的文件

//下面是在xuexi.xml中增加相应的记录信息
     XMLDocument1.LoadFromFile('xuexi/xuexi.xml');
     XMLDocument1.Active:=True;
     xn:=XMLDocument1.DocumentElement.AddChild('records');
     xn.SetAttributeNS('id','',edt1.Text);

     nTitle:=xn.AddChild('title');
     nTitle.Text:=edt1.Text;


     nDescription:=xn.AddChild('description');
     nDescription.Text:=edt2.Text;

     nWriteTime:=xn.AddChild('writeTime');
     nWriteTime.Text:=strDate;
     XMLDocument1.SaveToFile('xuexi/xuexi.xml');
     XMLDocument1.Free;
    

end;

添加xml数据可以通过XMLDocumen.xml.add();将数据分段加入,而保存就适应xmldocument.saveToFile();

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

再看一下删除xuexi.xml文件中的节点。

procedure TForm3.btn2Click(Sender: TObject);
var
  i,j,countChecked:Integer;
  strArray:array of string;

  begin
       j:=0;

       begin
          for i:=0 to lv1.Items.Count-1 do
            begin
             if  lv1.Items[i].Checked then
               begin

                 j:=j+1;
                 SetLength(strArray,j);
                 strArray[j-1]:=lv1.Items[i].Caption;
                
               end;

            end;
           
          for i:=Low(strArray) to High(strArray) do
            begin

               ShowMessage('将要删除的数据的caption:'+'序号:'+IntToStr(i)+'  '+strArray[i]);

            end;
          deleteFromListView(strArray);
          deleteFromXML(strArray);
          //deleteFile(strArray);

       end ;
       if Length(strArray)=0 then
       begin

          ShowMessage('no data be checked!');
          
       end;


  end;

procedure TForm3.deleteFromListView(const strArray:array of string);
var
  i,j:Integer;
  begin
   // ShowMessage('do in deleteFromListView'+IntToStr(lv1.Items.Count-1)+' '+IntToStr(Length(strArray)-1));
    i:=0;
    j:=0;
    for i:=Low(strArray) to High(strArray) do
      begin
        for j:=0 to lv1.Items.Count-1 do
          begin
            if strArray[i]=lv1.Items[j].Caption then
             begin
               lv1.Items[j].Delete;
               break;
             end;

          end;

      end;
  end;

procedure TForm3.deleteFromXML(const strArray:array of string);
var
  nodeList:IXMLNodeList;
  selectNode:IXMLNode;
  i,j:Integer;
  selectedIndex:Integer;
  begin
    //ShowMessage('进入deleteFromXML');
    nodeList:=XMLDocument1.DocumentElement.ChildNodes;
    for  i := Low(strArray) to High(strArray) do
      begin

        for j := 0 to nodeList.Count-1 do
          begin
             if nodeList.Get(j).GetAttributeNS('id','') = strArray[i] then
               begin                             
                  nodeList.Delete(j);
                  XMLDocument1.SaveToFile('xuexi/xuexi.xml');
                  Break;
               end;

          end;

      end;
    
  end;
本例子中listview中增加了checked,所以我通过的是checked来确定要删除的节点信息。删除的步骤为先删除listview中显示的别checked的节点,通过保存节点的title到一个动态的一维数组中,在遍历整个xml文件查找匹配的title,来删除。

猜你喜欢

转载自blog.csdn.net/zzlovelyboy/article/details/9797573