Sign in to follow this  
Followers 0

_GUICtrlListView_SaveXML() - Exports the details of a ListView to a .xml file.

12 posts in this topic

Posted (edited)

For those who enjoy using ListViews and wish to export the data to a XML file, then this function is for you.

Thanks to >Mat for the optimised XML format.

This is an example of the output for a ListView with 2 rows and 2 columns.

<?xml version="1.0" encoding="UTF-8" ?>
<listview rows="2" cols="2">
    <item>
        <column01>Row 1: Col 1</column01>
        <column02>Row 1: Col 2</column02>
    </item>
    <item>
        <column01>Row 2: Col 1</column01>
        <column02>Row 2: Col 2</column02>
    </item>
</listview>

Function without an array:

#include <Constants.au3>
#include <GUIListView.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListView_SaveXML
; Description ...: Exports a listview to a XML file.
; Syntax ........: _GUICtrlListView_SaveXML($hListView, $sFilePath[, $fDetails = Default])
; Parameters ....: $hListView           - Control ID/Handle to the control
;                  $sFilePath           - Filepath to save the XML data string to.
;                  $fDetails            - [optional] Print the number of columns & rows. Default is True.
; Return values .: Success - True
;                  Failure - False and sets @error to non-zero.
; Author ........: guinness
; Remarks .......: GUICtrlListView.au3 should be included.
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListView_SaveXML($hListView, $sFilePath, $fDetails = Default)
    Local Const $iColumnCount = _GUICtrlListView_GetColumnCount($hListView) - 1
    Local Const $iItemCount = _GUICtrlListView_GetItemCount($hListView) - 1
    Local $sDetails = ''
    If $fDetails Or $fDetails = Default Then
        $sDetails = ' rows="' & $iItemCount & '" cols="' & $iColumnCount & '"'
    EndIf
    Local $sXMLHeader = '<?xml version="1.0" encoding="UTF-8" ?>' & @CRLF & '<listview' & $sDetails & '>' & @CRLF
    $sDetails = ''

    Local $aColumns[$iColumnCount + 1], $aColumnText = 0 ; ListView Columns
    For $i = 0 To $iColumnCount
        $aColumnText = _GUICtrlListView_GetColumn($hListView, $i)
        $aColumns[$i] = StringStripWS(StringLower($aColumnText[5]), $STR_STRIPALL)
    Next

    Local $sReturn = ''
    For $i = 0 To $iItemCount ; ListView Items.
        $sReturn &= @TAB & '<item>' & @CRLF
        For $j = 0 To $iColumnCount
            $sReturn &= @TAB & @TAB & '<' & $aColumns[$j] & '>' & _GUICtrlListView_GetItemText($hListView, $i, $j) & '</' & $aColumns[$j] & '>' & @CRLF
        Next
        $sReturn &= @TAB & '</item>' & @CRLF
    Next
    $sReturn &= '</listview>' & @CRLF
    $sReturn = $sXMLHeader & $sReturn

    Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
    If $hFileOpen = -1 Then
        Return SetError(1, 0, False)
    EndIf
    FileWrite($hFileOpen, $sReturn)
    FileClose($hFileOpen)
    Return True
EndFunc   ;==>_GUICtrlListView_SaveXML

Function with an array: - It uses another function called >_GUICtrlListView_CreateArray()

#include <Constants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListView_SaveXMLEx
; Description ...: Exports a listview to a XML file.
; Syntax ........: _GUICtrlListView_SaveXMLEx(Const Byref $aArray, $sFilePath[, $fDetails = Default[, $sDelimiter = '|']])
; Parameters ....: $aArray              - [in/out and const] A 2-dimensional array returned by _GUICtrlListView_CreateArray.
;                  $sFilePath           - Filepath to save the XML data string to.
;                  $fDetails            - [optional] Print the number of columns & rows. Default is True.
;                  $sDelimiter          - [optional] Delimiter used as the _GUICtrlListView_CreateArray delimiter parameter. Default is '|'.
; Return values .: Success - True
;                  Failure - False and sets @error to non-zero.
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListView_SaveXMLEx(ByRef Const $aArray, $sFilePath, $fDetails = Default, $sDelimiter = '|')
    If $sDelimiter = Default Then
        $sDelimiter = '|'
    EndIf

    Local $sDetails = ''
    If $fDetails Or $fDetails = Default Then
        $sDetails = ' rows="' & $aArray[0][0] & '" cols="' & $aArray[0][1] & '"'
    EndIf
    Local $sXMLHeader = '<?xml version="1.0" encoding="UTF-8" ?>' & @CRLF & '<listview' & $sDetails & '>' & @CRLF
    $sDetails = ''

    Local $aColumns = StringSplit($aArray[0][2], $sDelimiter) ; ListView Columns
    For $i = 1 To $aColumns[0]
        $aColumns[$i] = StringStripWS(StringLower($aColumns[$i]), $STR_STRIPALL)
    Next

    Local Const $iColumnCount = $aArray[0][1] - 1
    Local $iColumnIndex = 0, $sReturn = ''
    For $i = 1 To $aArray[0][0] ; ListView Items.
        $sReturn &= @TAB & '<item>' & @CRLF
        For $j = 0 To $iColumnCount
            $iColumnIndex = $j + 1
            $sReturn &= @TAB & @TAB & '<' & $aColumns[$iColumnIndex] & '>' & $aArray[$i][$j] & '</' & $aColumns[$iColumnIndex] & '>' & @CRLF
        Next
        $sReturn &= @TAB & '</item>' & @CRLF
    Next
    $sReturn &= '</listview>' & @CRLF
    $sReturn = $sXMLHeader & $sReturn

    Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
    If $hFileOpen = -1 Then
        Return SetError(1, 0, False)
    EndIf
    FileWrite($hFileOpen, $sReturn)
    FileClose($hFileOpen)
    Return True
EndFunc   ;==>_GUICtrlListView_SaveXMLEx

Download the ZIP file to obtain all exporting UDFs as well as an example of usage. ListView Export.zip

Other Examples for exporting ListView data are: >_GUICtrlListView_SaveCSV(), >_GUICtrlListView_SaveHTML() & >_GUICtrlListView_SaveTxt()

Edited by guinness
1 person likes this

Share this post


Link to post
Share on other sites



Posted

Thats ugly and stupid, and that xml is never going to be pretty. Why not convert (slug is the term used in some places) the column headers to tags with a system for avoiding duplicates? Why not use properties rather than a new complex item called 'details'?

Share this post


Link to post
Share on other sites

Posted

Thats ugly and stupid

OK, XML is totally new to me, so do you have a better output Example that I could at least use please.

Share this post


Link to post
Share on other sites

Posted

Listview:

Id | Name
0 | Foo
1 | Bar

I'd go with:

<listview rows="2" cols="2">
    <item>
        <id>0</id>
        <name>Foo</name>
    </item>
    <item>
        <id>1</id>
        <name>Bar</name>
    </item>
</listview>

rows and cols should be optional. They aren't really needed.

Share this post


Link to post
Share on other sites

Posted (edited)

Brilliant, I will use that XML instead and post an update today. Thanks for the tip Mat.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Once again Mat thanks for pointing out the glaring mistake(s) with my XML format. I've updated the original post with your optimised XML format.

Share this post


Link to post
Share on other sites

Posted

Brilliant work! This will sure come in use with database management and should probs go on onw of the UDFs.

Share this post


Link to post
Share on other sites

Posted

if program stops responding what can i code

Share this post


Link to post
Share on other sites

Posted

if program stops responding what can i code

Pardon? Could you try to explain a little more in depth what your problem is with the code. Thanks,

Share this post


Link to post
Share on other sites

Posted

yes

no problem yet

just wished i had a back up plan for the future

Share this post


Link to post
Share on other sites

Posted

yes

no problem yet

just wished i had a back up plan for the future

OK, I'm lost but good luck.

Share this post


Link to post
Share on other sites

Posted (edited)

After 2 years and 750+ downloads, I decided now was the time to release a new version of exporting to a XML format from a listview. An example as well as all the exporting UDFs (minus the HTML for now) can be found in the ZIP file. Download and enjoy!

Edited by guinness

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.