Menu

XMLReader example in VCLua 0.5.x

Lazarus has some interesting examples, one of this is the XMLReader.
But who wants waste time to make gui forms with script language? Much easier to make it with a standard formdesigner and just load it on runtime...

-Use "Save Form as XML" option in Lazarus Ide on a form.
-Check the XML, remove any 'binary' tags. ( yes, this is a bug in lazarus ide )
-Finally write your lua script in 5 mins. or less

(This sample works in current development {v0.5})

require "vcl"
VCL = vcl

--XMLFormToLua loads the exported XML form and converts it to lus script.
loadstring(VCL.XMLFormToLua("testform.xml"))()

-- Load button event
function reLoad()
    TreeView1:Clear()
    if TreeView1:LoadXML(FileNameEdit1.FileName) then else VCL.ShowMessage("ERROR") end
end
BitBtn1.OnClick = "reLoad"

-- The TreeViewClick event handler
-- (ListView replaced with StringGrid)
function OnTreeClick(s)
    if s:Selected() then
        ListView1:Clear()
        ListView1:LoadRowFromTable(0,{"Name","Value"})
        t = s:GetDOMNode(s:Selected())
        if t then
            for k,v in pairs(t) do          
                if type(v)=="table" then
                    for kk,vv in pairs(v) do
                        -- use addrow(n) to insert at position instead of append with addrow()
                        ListView1:LoadRowFromTable(ListView1:AddRow(1),{kk,vv})
                    end
                else
                    -- DOMNode.NodeName and NodeValue
                    if k=="Name" then
                        Edit1.Text= v or ""
                    elseif k=="Value" then
                        Edit2.Text= v or ""
                    end                 
                end
            end
        end
    end
end
TreeView1.OnClick = "OnTreeClick"

-- Finally
XMLReaderForm:ShowModal()

The result is same as the original example...

Posted by Janos Bali 2014-04-10 Labels: XMLFormToLua TreeView LoadXML
  • refi

    refi - 2014-12-03

    Hi Balijani,

    the lua script is displaying an error:

    G:\VCLXML>lua52 VCLua-XML.lua
    lua52: VCLua-XML.lua:8: bad argument #1 to 'loadstring' (function expected, got
    table)
    stack traceback:
    [C]: in function 'loadstring'
    VCLua-XML.lua:8: in main chunk
    [C]: in ?

    Can you help?

    Thanks

    Refi

     
  • Janos Bali

    Janos Bali - 2014-12-03

    Hi!

    This script was written before the latest version released, so its have some bugs.
    The XMLFormToLua function returns table, not string.

    change

    loadstring(VCL.XMLFormToLua("testform.xml"))()

    to

    local _script = VCL.XMLFormToLua("testform.xml")
    _script[1] = "XMLRederForm = VCL.Form()" -- change the mainform name!!!
    loadstring(table.concat(_script,"\n"))()

    events no longer accepts strings, modify these lines

    BitBtn1.OnClick = "reLoad" --> BitBtn1.OnClick = reLoad
    TreeView1.OnClick = "OnTreeClick" -- > TreeView1.OnClick = OnTreeClick

    finally check the form name
    XMLReaderForm:ShowModal() will not work, but XMLRederForm:ShowModal() do

    regards bj

     

    Last edit: Janos Bali 2014-12-03
  • refi

    refi - 2014-12-04

    Hi Janos Bali,

    Now the form is displayed, and can load XML file into the treeview. However selecting any tree item an error pops up: "attempt to call method 'LoadRowFromTable' (a nil value)".

    Thanks for your help.

    Refi

     
  • Janos Bali

    Janos Bali - 2014-12-04

    see comment
    -- (ListView replaced with StringGrid)

    another change in 0.5.0

    following code will not work, because selected returns table of selected nodes
    t = s:GetDOMNode(s:Selected())
    if t then
    for k,v in pairs(t) do

    you should use
    t = s:Selected()
    if type(t)=="table" and t[1] then
    node = s:GetDOMNode(t[1].AbsoluteIndex)
    for k,v in pairs(node) do

    instead

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.