Unit1.~cpp
上传用户:wangli
上传日期:2008-04-19
资源大小:3077k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "my_xml.h"
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. #include "Unit2.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. TListColumn *NewColumn;//创建一个栏 
  12. TListItem *ListItem;//创建一个Items 
  13. //---------------------------------------------------------------------------
  14. __fastcall TForm1::TForm1(TComponent* Owner)
  15.         : TForm(Owner)
  16. {
  17. }
  18. //---------------------------------------------------------------------------
  19. void __fastcall TForm1::Button1Click(TObject *Sender)
  20. {
  21.  /* myXML xml;
  22.   xml.init("1.xml");
  23.    xmlNode root = xml.root();
  24.    //xmlNode node = root.get_child_node("item1");
  25.    node.set_attribute("日期","2004-11-24");
  26.    node.set_attribute("方式","支出");
  27.    node.set_attribute("金额","88");
  28.    node.set_attribute("说明","买东西");
  29.    node = root.get_child_node("体重");
  30.    ShowMessage(node.value());
  31.    root.set_child_value("身长","'180");
  32.    xml.save();  */
  33. int ind=lstMain->Selected->Index; 
  34. //ind为要删除的节点号 
  35. _di_IXMLNode node=XMLDoc->DocumentElement; 
  36. int re=node->ChildNodes->Delete(ind);
  37. XMLDoc->SaveToFile(WideString("1.xml"));
  38. if (re!=-1) 
  39. lstMain->Items->Delete(ind);
  40.  
  41. //--------------------------------------------------------------------------- 
  42. void __fastcall TForm1::FormCreate(TObject *Sender)
  43. {
  44. /*
  45. lstMain->ViewStyle = vsReport;//定义显示方式
  46. NewColumn = lstMain->Columns->Add();//增加栏
  47. NewColumn->Caption = "序号";//添置栏的名称
  48. NewColumn->Width = 40;
  49. NewColumn = lstMain->Columns->Add();//增加栏
  50. NewColumn->Caption = "日期";//添置栏的名称
  51. NewColumn->Width = 80;
  52. NewColumn = lstMain->Columns->Add();
  53. NewColumn->Caption = "方式";
  54. NewColumn = lstMain->Columns->Add();
  55. NewColumn->Caption = "金额";
  56. NewColumn->Width = 80;
  57. NewColumn = lstMain->Columns->Add();
  58. NewColumn->Caption = "说明";
  59. NewColumn->Width = 300;   */
  60. //const TRect &Rect;
  61. /*  const char Names[6][3][10] ={
  62. {"广东省","广州市","华南"}, 
  63. {"上海市", "上海市","华东"}, 
  64. {"北京市", "北京市","华北"}, 
  65. {"辽宁省", "沈阳市","东北"}, 
  66. {"湖北省", "武汉市","华中"}, 
  67. {"云南省", "昆明市","西南"}};//准备各项的文字
  68. for (int i = 0; i < 6; i++) 
  69. ListItem = ListView1->Items->Add(); 
  70. ListItem->Caption = Names[i][0];//添置父节点 
  71. ListItem->SubItems->Add(Names[i][1]);//添置子节点
  72. ListItem->SubItems->Add(Names[i][2]);
  73. }  */
  74.  /* myXML xml;
  75.    xml.init("1.xml");
  76.    xmlNode root = xml.root();
  77.    xmlNode node = root.get_child_node("item1");
  78. ListItem = ListView1->Items->Add();
  79. ListItem->Caption = node.attribute("日期");//添置父节点
  80. ListItem->SubItems->Add(node.attribute("方式"));//添置子节点
  81. ListItem->SubItems->Add(node.attribute("金额"));
  82. ListItem->SubItems->Add(node.attribute("说明"));   */
  83. refresh();
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TForm1::ListView1CustomDrawItem(TCustomListView *Sender,
  87.       TListItem *Item, TCustomDrawState State, bool &DefaultDraw)
  88. {
  89. /*if(ListView1->Selected)
  90. {
  91. ListView1->ClearSelection(); 
  92. Sender->Canvas->Brush->Color=clBlue; 
  93. } */
  94. //ListView1->Canvas->Brush->Color  =  clBtnFace;
  95.   /* if  (Item->Index%2  !=  0)
  96.                ListView1->Canvas->Brush->Color  =  clBtnFace;   */
  97. }
  98. //---------------------------------------------------------------------------
  99. bool __fastcall TForm1::AddTask(int itemnum,String date,
  100. String method,int account,String con)
  101. {
  102. //TODO: Add your source code here
  103. _di_IXMLNode node=XMLDoc->DocumentElement;
  104. node=node->AddChild(WideString("Tip"));
  105. _di_IXMLNode subnode=node->AddChild(WideString("item"));
  106. subnode->SetText(WideString(itemnum));
  107. subnode=node->AddChild(WideString("date"));
  108. subnode->SetText(WideString(date));
  109. subnode=node->AddChild(WideString("method"));
  110. subnode->SetText(WideString(method));
  111. subnode=node->AddChild(WideString("account"));
  112. subnode->SetText(WideString(account));
  113. subnode=node->AddChild(WideString("Content"));
  114. subnode->SetText(WideString(con));
  115. XMLDoc->SaveToFile(WideString("1.xml"));
  116. return true;
  117. } //以上代码很容易理解,读的时候可以对照XML文件的格式
  118. void __fastcall TForm1::Button2Click(TObject *Sender)
  119. {
  120. //AddTask(1,Date(),"支出",33,"lkdlkdfklkl");
  121. Form2->Show();
  122. //
  123. Form1->refresh();
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::refresh()
  127. {
  128. XMLDoc->LoadFromFile(WideString("1.xml")); 
  129. //XMLDoc是TXMLDocument的名字 
  130. XMLDoc->Active=true; 
  131. //FillList(); //把XML的内容添加到ListView 
  132. //然后就是如何读取XML的内容(FillList): 
  133. lstMain->Items->BeginUpdate(); 
  134. lstMain->Items->Clear(); 
  135. _di_IXMLNode node=XMLDoc->DocumentElement; 
  136. //DocumentElement属性用来得到根节点 
  137. _di_IXMLNodeList nodes=node->ChildNodes; 
  138. //得到子节点List
  139. for(int n=0;n<nodes->Count;n++) 
  140. TListItem* li=lstMain->Items->Add(); 
  141. node=nodes->Nodes[n]->ChildNodes->Nodes[0]; 
  142. //得到子节点的子节点,也也就是 VTip//Tip//Mode
  143. int m=String(node->GetText()).ToInt(); 
  144. //得到对应节点的Text内容并转换到Int 
  145. li->Caption=m; 
  146. li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[1]->GetText()); 
  147. li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[2]->GetText());
  148. li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[3]->GetText());
  149. li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[4]->GetText());
  150. //代码同上 
  151. lstMain->Items->EndUpdate(); 
  152. }