Jump to content

[Resolved] Listview to xml


Recommended Posts

Hi,

Searching the Help file and this forum I've been able to load a xml file into a ListView. But, I'm creating a Listview Editor so the the listview items can be added to or edited. Now, I can not figure out how to save the Listview items to a xml file?

My xml file and AutoIt script are below.

My xml file:

CODE
<?xml version="1.0" encoding="ISO-8859-1"?>

<menu title="XMLMenu">

<item id="id1" fname="fname1" lname="lname1"/>

<item id="id2" fname="fname2" lname="lname2"/>

<item id="id3" fname="fname3" lname="lname3"/>

<item id="id4" fname="fname4" lname="lname4"/>

<item id="id5" fname="fname5" lname="lname5"/>

</menu>

My AutoIt Script: [Edit 07/05/2009 - Working Code]

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include "_XMLDomWrapper.au3"

Global $hInput[100], $HotItem

$sFile = @ScriptDir & "\ListviewXML1d.xml"
$sRead = FileRead($sFile)

$aid = StringRegExp($sRead, '(?i)id="(.*?)"', 3)
If @error Then Exit

$afname = StringRegExp($sRead, '(?i)fname="(.*?)"', 3)
If @error Then Exit

$alname = StringRegExp($sRead, '(?i)lname="(.*?)"', 3)
If @error Then Exit

$iUbound = UBound($aid)
;If UBound($aItem) > $iUbound Then $iUbound = UBound($aItem)

GUICreate("XML to ListView Editor Demo", 400, 300)



$hListView = GUICtrlCreateListView("My ID | First Name | Last Name", 10, 10, 280, 180)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;Here's the trick

_GUICtrlListView_RegisterSortCallBack($hListView)

    
For $i = 0 To $iUbound - 1
    $strData = ""
    If UBound($aid) > $i Then $strData &= $aid[$i]
    If UBound($afname) > $i Then $strData &= "|" & $afname[$i]
    If UBound($alname) > $i Then $strData &= "|" & $alname[$i]
    GUICtrlCreateListViewItem($strData, $hListView)
Next


;=======================================================================================================================

; BUTTONS - Reset, Clear
$btnAdd = GUICtrlCreateButton("Add", 90, 220, 35, 20)
$btnEdit = GUICtrlCreateButton("Edit", 130, 220, 35, 20)
$btnDelete = GUICtrlCreateButton("Delete", 170, 220, 40, 20)
$btnSave = GUICtrlCreateButton("Save", 215, 220, 35, 20)
$btnExit = GUICtrlCreateButton("Exit", 255, 220, 35, 20)


$hInput[1] = GUICtrlCreateInput("", 10, 195, 70, 20)
$hInput[2] = GUICtrlCreateInput("", 85, 195, 70, 20)                    
$hInput[3] = GUICtrlCreateInput("", 160, 195, 80, 20)                   

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE 
            Exit
        Case $btnExit   
            Exit
        Case $hListView
            _GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))     
        Case $btnDelete
            ;
            _Delete()
        Case $btnSave
            ;
            _Save()
    EndSwitch

WEnd 

;=======================================================================================================================

Func _Delete()
    ; Delete Hot Item (Selected Item)
    ;~ MsgBox(0, "", "Hot Item #: " & $HotItem)
    _GUICtrlListView_DeleteItem($hListView, $HotItem)
    
EndFunc     ;==>_Delete

Func _Save()
    ;   
    Const $FileName = "ListviewXML1d.xml"

    Global $Keys[3] =  ["id","fname","lname"]
    Global $Values[3]
    
    ;Create file (force overwrite existing file)
    $result = _XMLCreateFile($FileName, "menu", 1)

    Switch @error
        Case 0 
            ConsoleWrite("No error" & @CRLF)
        Case 1 
            ConsoleWrite("Failed to create file" & @CRLF)
        Case 2 
            ConsoleWrite("No object" & @CRLF)
        Case 3 
            ConsoleWrite("File creation failed MSXML error" & @CRLF)
        Case 4 
            ConsoleWrite("File exists" & @CRLF)
    EndSwitch

    ;Open created file
    _XMLFileOpen($FileName)

    ;_XMLCreateRootChild ("item", "")
    
    For $i = 0 To _GUICtrlListView_GetItemCount($hListView) -1
        ;$Values = _GUICtrlListView_GetItemTextArray($hListView, $i)
        
            $Values[0] = _GUICtrlListView_GetItemText($hListView, $i, 0)
            $Values[1] = _GUICtrlListView_GetItemText($hListView, $i, 1)
            $Values[2] = _GUICtrlListView_GetItemText($hListView, $i, 2)            
            ConsoleWrite($Values[0] & " " & $Values[1] & " " & $Values[2] & @LF)    
            ;~MsgBox(0, "", $Values[0])
            
            _XMLCreateChildWAttr ("//menu", "item", $Keys, $Values)
            ;_XMLSetAttrib("//menu", $Keys, $Values)        
    Next
        
EndFunc     ;==>_Save

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                
                
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    
                     Global $aItem, $Status, $hInput
                     $a_Item = _GUICtrlListView_GetItemTextArray($hListView)
            
                        If IsArray($a_Item) Then
                            ;MsgBox(0, "", $a_Item)
                            GUICtrlSetData($Status, "")
                            GUICtrlSetData($Status, $a_Item[1])
                            GUICtrlSetData($hInput[1], $a_Item[1])
                            GUICtrlSetData($hInput[2], $a_Item[2])
                            GUICtrlSetData($hInput[3], $a_Item[3])
                            
                            ; Get seleted item ID Number
                            $HotItem = _GUICtrlListView_GetHotItem($hListView)
                        EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Thank you for your help,

jfcby

Edited by jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...