Saving Tree to a file

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
HGAutomator
Posts: 62
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF
Has thanked: 20 times
Been thanked: 9 times

Saving Tree to a file

Post by HGAutomator »

Hi,

I've looked at all the Tree Samples, and the only SaveAs option is a bitmap. Are there are any existing snippets for saving a tree to xml or json, or to a database?

User avatar
serge_girard
Posts: 2771
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 1067 times
Been thanked: 193 times
Contact:

Post by serge_girard »

I would like to have that too!
Serge Now Is The Happiest Time Of Your Life !

edk
Posts: 708
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 193 times
Been thanked: 650 times

Post by edk »

I don't think there is a ready solution for exporting / importing trees, but you can easily build your own solutions.
Below I present the code that allows you to write and read the tree structure to a file in json format.
It is not a ready-made universal tool and it will certainly not work in all applications, but it shows how the tree structure export / import operation can be quite easily accomplished.
I chose the json format so that tasks would be performed on arrays, but you can successfully (with a little modification) use a DBF file.
Tree_99.7z
(4.11 KiB) Downloaded 70 times
For exercise purposes, try to expand the export / import functions with e.g. information which nodes are expanded and which are collapsed. :mrgreen:

By the way, I found a strange bug in the DeleteAllItems method. It has something to do with the use of the GetFile / PutFile function, but I can't find the reason. You can observe this error by following these steps:
For the GetFile function:
1. Click the "Delete All / Add Test" button
2. Click the "Load Tree Stru" button
3. Cancel loading the file.
4. Click the "Delete All Items" button

For the PutFile function:
1. Click the "Delete All / Add Test" button
2. Click the "Save Tree Stru" button
3. Cancel saving the file.
4. Click the "Delete All Items" button

For this reason, in my tree structure import function, I used this snippet of code instead of the DeleteAllItems method:

Code: Select all

Do While GetProperty ( cFormName, cTreeName, "ItemCount" ) > 0
     DO Events
     DoMethod( cFormName, cTreeName, "DeleteItem", GetProperty ( cFormName, cTreeName, "ItemCount" ) )
Enddo
Enjoy.

User avatar
serge_girard
Posts: 2771
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 1067 times
Been thanked: 193 times
Contact:

Post by serge_girard »

Thanks !
Serge Now Is The Happiest Time Of Your Life !

HGAutomator
Posts: 62
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF
Has thanked: 20 times
Been thanked: 9 times

Post by HGAutomator »

Very nice, that's a start. Thanks, edk.

User avatar
AidTIC
Posts: 103
Joined: Mon Apr 22, 2013 8:29 am
DBs Used: DBF
Been thanked: 4 times
Contact:

Post by AidTIC »

Sabríais si hay algún tamaño máximo para variables json?

viewtopic.php?f=24&t=6925&p=66502&hilit=json#p66502
Skydone Solutions
www.skydone.com

Post Reply