xml删读写源代码

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <StatusBarConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
$strSourceFile = "Contacts.xml"
$brstr= chr(13)&chr(10)&chr(9)
$Title = "XML 通讯录"
$Form1 = GUICreate($Title, 412, 338)
GUICtrlCreateGroup("", 8, 32, 395, 283)
$ListView1 = GUICtrlCreateListView("ID|姓 名|电 话", 16, 48, 193, 257)
GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
_GUICtrlListView_SetColumnWidth($ListView1, 0, 30)
_GUICtrlListView_SetColumnWidth($ListView1, 1, 60)
$menu = GUICtrlCreateContextMenu($ListView1)
$e_menu = GUICtrlCreateMenuItem("编辑", $menu)
$d_menu = GUICtrlCreateMenuItem("删除", $menu)
GUICtrlCreateMenuItem("", $menu)
$c_menu = GUICtrlCreateMenuItem("刷新", $menu)
GUICtrlCreateLabel("姓名:", 216, 50, 40, 17)
$Name_input = GUICtrlCreateInput("", 258, 47, 137, 21)
GUICtrlCreateLabel("电话:", 216, 80, 40, 17)
$Tel_Input = GUICtrlCreateInput("", 258, 77, 137, 21)
GUICtrlCreateLabel("地址:", 216, 110, 40, 17)
$Address_Input = GUICtrlCreateInput("", 258, 107, 137, 21)
GUICtrlCreateLabel("邮箱:", 216, 140, 40, 17)
$Email_Input = GUICtrlCreateInput("", 258, 137, 137, 21)
GUICtrlCreateLabel("OICQ:", 216, 170, 40, 17)
$OICQ_Input = GUICtrlCreateInput("", 258, 167, 137, 21)
$Remarks_Edit = GUICtrlCreateEdit("", 216, 197, 179, 108)
GUICtrlSetData(-1, "备注信息")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$A_button = GUICtrlCreateButton("添加记录", 8, 8, 57, 24,$BS_FLAT)
$E_button = GUICtrlCreateButton("编辑记录", 81, 8, 57, 24,$BS_FLAT)
$D_button = GUICtrlCreateButton("删除记录", 153, 8, 57, 24,$BS_FLAT)
$S_Input = GUICtrlCreateInput("这里输入查找条件", 232, 10, 129, 21)
$S_button = GUICtrlCreateButton("查找", 369, 8, 33, 24, $BS_FLAT)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
GUISetState(@SW_SHOW)
;~ R_xml()
_GUICtrlListView_RegisterSortCallBack($ListView1)
While 1
 $nMsg = GUIGetMsg()
 Select
  Case $nMsg =$GUI_EVENT_CLOSE
    Exit
        Case $nMsg =$GUI_EVENT_PRIMARYDOWN
   $pos = GUIGetCursorInfo();返回数组$pos[4] 表示鼠标下面的控件的控件ID( 0 为没有或者无法获取)
                if $pos <> 0 then
                     if ($pos[4] == $listview1) Then V_xml()
       EndIf
  Case $nMsg = $ListView1
    _GUICtrlListView_SortItems($ListView1, GUICtrlGetState($ListView1))
        Case $nMsg =$A_button
             A_GUI(0,0,"","","","","","")
        Case $nMsg = $c_menu
             _GUICtrlListView_DeleteAllItems($ListView1)
             R_xml()
        Case $nMsg = $d_menu or $nMsg = $D_button
             $id = _GUICtrlListView_GetItemText($ListView1, _GUICtrlListView_GetNextItem($ListView1), 0)
             if $id <> "" then D_xml($id)
  Case $nMsg = $E_button or $nMsg = $e_menu
    $id = _GUICtrlListView_GetItemText($ListView1, _GUICtrlListView_GetNextItem($ListView1), 0)
             if $id <> "" then
             $name = GUICtrlRead($Name_input)
             $tel = GUICtrlRead($tel_input)
             $address = GUICtrlRead($address_input)
    $email = GUICtrlRead($email_input)
    $oicq = GUICtrlRead($oicq_input)
             $remarks = GUICtrlRead($Remarks_Edit)
             A_GUI(1,$id,$name,$tel,$address,$email,$oicq,$remarks)
             Else
                 MsgBox(64,$Title,"请先选中一条记录在进行操作!!!")
             EndIf
        Case $nMsg = $S_button
             $key = GUICtrlRead($S_Input)
   if $key <> "" And $key <> "这里输入查找条件" then
                 S_xml($key)
                Else
                 MsgBox(64,$Title,"请先输入查找人的姓名或电话,可模糊查找!!!")
      EndIf
 EndSelect
WEnd
_GUICtrlListView_UnRegisterSortCallBack($ListView1)

Func A_GUI($var,$id,$name,$tel,$address,$email,$oicq,$remarks)
if $var = 0 then $Form2_title = "添加"
if $var = 1 then $Form2_title = "编辑"
$Form2 = GUICreate($Form2_title&"资料", 297, 223)
GUICtrlCreateGroup("", 7, 5, 281, 208)
GUICtrlCreateLabel("姓   名:", 27, 25, 60, 17)
GUICtrlCreateLabel("电   话:", 27, 49, 60, 17)
GUICtrlCreateLabel("地   址:", 27, 71, 60, 17)
GUICtrlCreateLabel("邮   箱:", 27, 93, 60, 17)
GUICtrlCreateLabel("Q i C Q:", 27, 115, 60, 17)
$Input1 = GUICtrlCreateInput($name, 82, 23, 185, 21)
$Input2 = GUICtrlCreateInput($tel, 82, 45, 185, 21)
$Input3 = GUICtrlCreateInput($address, 82, 66, 185, 21)
$Input4 = GUICtrlCreateInput($email, 82, 88, 185, 21)
$Input5 = GUICtrlCreateInput($oicq, 82, 110, 185, 21)
$Edit1 = GUICtrlCreateEdit("", 27, 136, 241, 41)
GUICtrlSetData(-1, $remarks)
$save = GUICtrlCreateButton("保   存", 40, 184, 65, 20, 0)
$exit = GUICtrlCreateButton("退   出", 191, 184, 65, 20, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
 $Msg = GUIGetMsg()
 Select
  Case $Msg = $GUI_EVENT_CLOSE or $Msg = $exit
    GUIDelete($Form2)
    WinActivate("客户资料录入")
    GUISetState(@SW_ENABLE, $Form1)
    ExitLoop
        Case $Msg = $save
   $name = GUICtrlRead($Input1)
   $tel = GUICtrlRead($Input2)
   $address = GUICtrlRead($Input3)
   $email = GUICtrlRead($Input4)
   $oicq = GUICtrlRead($Input5)
   $remarks = GUICtrlRead($Edit1 )
             if $var = 0 then
                if $name="" or $tel="" or $address ="" Or $email = "" or $oicq = "" or $remarks = "" then
                  MsgBox(64,$Form2_title&"资料","资料不完整!")
                  Else
         A_xml($name,$tel,$address,$email,$oicq,$remarks)
      MsgBox(64,$Form2_title&"资料","资料添加成功")
    EndIf
    EndIf
             if $var = 1 then
                  if $name= "" or $tel= "" or $address = "" Or $email = "" or $oicq = "" or $remarks = "" then
                  MsgBox(64,$Form2_title&"资料","资料不完整!")
                  Else
         E_xml($ID,$name,$tel,$address,$email,$oicq,$remarks)
      MsgBox(64,$Form2_title&"资料","资料编辑成功!")
      EndIf
    EndIf
    EndSelect
WEnd
EndFunc

Func R_xml();读xml(所有资料)函数
$objXML = ObjCreate("Microsoft.XMLDOM")
$sFile = $objXML.load($strSourceFile)
if not $sFile then
MsgBox(16,$Title,"加载"&$strSourceFile&"失败,文件不存在!!!")
Else
$objRootsite = $objXML.documentElement.selectSingleNode("NewList")
$AllNodesNum = $objRootsite.childNodes.length - 1
 if $AllNodesNum < 0 then
        _GUICtrlStatusBar_SetText ($StatusBar1,"  没有找到任何记录,请先添加!!!                 ")
        Else
         For $i = 0 to $AllNodesNum
      $id = $i+1
         $Name = $objRootsite.childNodes.item($i).childNodes.item(0).text
         $Tel = $objRootsite.childNodes.item($i).childNodes.item(1).text
         $Address = $objRootsite.childNodes.item($i).childNodes.item(2).text
         $Email = $objRootsite.childNodes.item($i).childNodes.item(3).text
         $OICQ = $objRootsite.childNodes.item($i).childNodes.item(4).text
         $Remarks = $objRootsite.childNodes.item($i).childNodes.item(5).text
         GUICtrlCreateListViewItem($id & '|'& $Name & '|'&$Tel, $ListView1 )
         _GUICtrlStatusBar_SetText ($StatusBar1,"  共找到 "&_GUICtrlListView_GetItemCount($listview1)&" 条记录          ")
         Next
    EndIf
EndIf
EndFunc


Func A_xml($name,$Tel,$Address,$Email,$Oicq,$Remarks);添加记录函数
$objXML = ObjCreate("Microsoft.XMLDOM");创建XML对像
$sFile = $objXML.load($strSourceFile);把XML文件读入内存中
if not $sFile then
MsgBox(16,$Title,"加载"&$strSourceFile&"失败,文件不存在!!!")
Else
$objRootlist = $objXML.documentElement.selectSingleNode("NewList");选取<NewList>节点
$XMLnode = $brstr&"<list>"&$brstr&"<name>"&$name&"</name>"&$brstr&"<tel>"&$Tel&"</tel>"&$brstr& _
"<address>"&$Address&"</address>"&$brstr&"<email>"&$email&"</email>"&$brstr& _
"<oicq>"&$oicq&"</oicq>"&$brstr&"<remarks>"&$Remarks&"</remarks>"&$brstr&"</list>"&chr(13)
$objXML2= ObjCreate("Microsoft.XMLDOM");建立一个新XML对像
$objXML2.loadXML($XMLnode);把XML版片段读入内存中
$rootNewNode=$objXML2.documentElement;获得objXML2的根节点
$objRootlist.appendChild($rootNewNode);把XML片段插入到xml中
$objXML.save($strSourceFile);存储xml文件(因为不存储xml只在内存中更新了)
MsgBox(64,$Title,"恭喜,记录添加成功!!!")
_GUICtrlListView_DeleteAllItems($ListView1)
R_xml()
endif
EndFunc

Func V_xml()
         $ID = _GUICtrlListView_GetItemText($ListView1, _GUICtrlListView_GetNextItem($ListView1), 0)
         If $ID <> "" Then
             GUICtrlSetData($Name_Input,_GUICtrlListView_GetItemText($ListView1, _GUICtrlListView_GetNextItem($ListView1), 1))
    GUICtrlSetData($Tel_Input,_GUICtrlListView_GetItemText($ListView1, _GUICtrlListView_GetNextItem($ListView1), 2))
             $objXML = ObjCreate("Microsoft.XMLDOM")
    $objXML.load($strSourceFile)
             $objRootsite = $objXML.documentElement.selectSingleNode("NewList")
             $AllNodesNum = $objRootsite.childNodes.length - 1
    $Address = $objRootsite.childNodes.item($ID-1).childNodes.item(2).text
             $Email = $objRootsite.childNodes.item($ID-1).childNodes.item(3).text
             $OICQ = $objRootsite.childNodes.item($ID-1).childNodes.item(4).text
             $Remarks = $objRootsite.childNodes.item($ID-1).childNodes.item(5).text
             GUICtrlSetData($Address_Input,$Address)
             GUICtrlSetData($Email_Input,$Email)
             GUICtrlSetData($OICQ_Input,$OICQ)
             GUICtrlSetData($Remarks_Edit,$Remarks)
  EndIf 
EndFunc

Func E_xml($ID,$name,$tel,$address,$email,$oicq,$remarks)
$objXML = ObjCreate("Microsoft.XMLDOM")
$sFile = $objXML.load($strSourceFile)
$objRootsite = $objXML.documentElement.selectSingleNode("NewList")
$objRootsite.childNodes.item($ID-1).childNodes.item(0).text = $name
$objRootsite.childNodes.item($ID-1).childNodes.item(1).text = $tel
$objRootsite.childNodes.item($ID-1).childNodes.item(2).text = $address
$objRootsite.childNodes.item($ID-1).childNodes.item(3).text = $email
$objRootsite.childNodes.item($ID-1).childNodes.item(4).text = $oicq
$objRootsite.childNodes.item($ID-1).childNodes.item(5).text = $remarks
$objXML.save($strSourceFile)
EndFunc

Func D_xml($ID)
$objXML = ObjCreate("Microsoft.XMLDOM");创建XML对像
$sFile = $objXML.load($strSourceFile)
$objRootsite = $objXML.documentElement.selectSingleNode("NewList")
$objNode = $objRootsite.childNodes.item($id-1)
$objRootsite.RemoveChild($objNode)
$objXML.save($strSourceFile)
_GUICtrlListView_DeleteAllItems($ListView1)
R_xml()
MsgBox(64,$Title,"恭喜,记录删除成功!!!")
EndFunc

Func S_xml($key)
_GUICtrlListView_DeleteAllItems($ListView1)
$objXML = ObjCreate("Microsoft.XMLDOM")
$sFile = $objXML.load($strSourceFile)
$objRootsite = $objXML.documentElement.selectSingleNode("NewList")
$AllNodesNum = $objRootsite.childNodes.length - 1
 if $AllNodesNum < 0 then
        _GUICtrlStatusBar_SetText ($StatusBar1,"  没有找到任何记录,请先添加!!!                 ")
        Else
         For $i = 0 to $AllNodesNum
      $id = $i+1
         $Name = $objRootsite.childNodes.item($i).childNodes.item(0).text
         $Tel = $objRootsite.childNodes.item($i).childNodes.item(1).text
   if StringInStr ($name, $key) <> 0 or StringInStr ($tel, $key) <> 0 then GUICtrlCreateListViewItem($id & '|'& $Name & '|'&$Tel, $ListView1 )   
      Next
   _GUICtrlStatusBar_SetText ($StatusBar1,"  共找到 "&_GUICtrlListView_GetItemCount($listview1)&" 条记录          ")
    EndIf
EndFunc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值