murgaLua libraries

Reference ››
Parent Previous

XML functionality

saveToXml( fileName, variableName )

Usage:



local table_1 = {firstVar=1; secondVar=2; thirdVar="abc"}

local myVar = "This is a string"

local table_2 = {myVar, "Test String", true, table_1}


local myTable = {firstKey="simpleItem"; secondKey=table_2}


murgaLua.saveToXml("foo.xml", myTable)




As we see in the example we have defined a series of data structures and wrapped a table around them ...


And the function allows you to save all this data to a human readable XML file.


loadFromXml( fileName )

Usage:



local loadedTable = murgaLua.loadFromXml("foo.xml")




Returns a table will all the data just like we saved it before.


importXmlFile( fileName )

Usage:



local xmlTable = murgaLua.importXmlFile("test.xml")


print( "Root Element Name : " .. xmlTable[1].name )




Loads and parses any valid XML file (see xmlImport.lua test).

importXmlString( fileName )

Usage:



local xmlTable = murgaLua.importXmlString("<test height=2>text</test>")


print( "Element Name     : " .. xmlTable[1].name )

print( "Element contents : " .. xmlTable[1][1])

print( "Attribute Name   : " .. next(xmlTable[1].attr))

print( "Attribute Value  : " .. xmlTable[1].attr[next(xmlTable[1].attr)])




Loads and parses any valid XML file (see xmlImport.lua test).


exportXml( xmlTable, fileHandle )

Usage:



diskfile = io.open("test.xml", "w")

murgaLua.exportXml(xmlTable, diskfile)

diskfile:close()




Saves the structure created by importXml* ... This has to be better documented (see xmlImport.lua test).


exportXml( xmlTable, fileHandle )

Usage:



diskfile = io.open("test.xml", "w")

murgaLua.exportXml(xmlTable, diskfile)

diskfile:close()




Saves the structure created by importXml* ... This has to be better documented (see xmlImport.lua test).


findXmlNode( xmlTable, pathToNode )

Usage:



subStruct = murgaLua.findXmlNode(xmlTable, "/example/subStruct")




Allows you to search for the part of the XML table that you are interested in (see xmlImport.lua test).


findNextXmlNode( xmlTable, pathToNode )

Usage:



subStruct = murgaLua.findXmlNode(xmlTable, "/example/subStruct")

repeatingNode = murgaLua.findNextXmlNode(subStruct, "repeatingNode")




Allows you to get nodes that repeat inside an XML structure (see xmlImport.lua test).


Utility functions

These functions are included to ease debugging and make filtering tables a little easier.


getHostOsName()

Usage:



murgaLua.getHostOsName()




Returns the OS murgaLua was build for, currently one of "linux", "windows" or "macos".


sleep(miliseconds)

Usage:



murgaLua.sleep(1000)




Sleeps for the specified number of miliseconds without taxing the system.


newIoString()

Usage:



function write_squares_table(file)

 for i = 1,10 do

   file:write(i, " ", i*i, "\n")

 end

end


-- write to file:


diskfile = io.open("squares", "w")

write_squares_table(diskfile)

diskfile:close()


-- write to string:


stringfile = murgaLua.newIoString()

write_squares_table(stringfile)

print(stringfile:getstring())




Creates a new iostring - need to document more (see xmlImport.lua test).


createFltkTimer()

Usage:



timer = murgaLua.createFltkTimer()

timer:callback(sleep_callback)




Allows you to do neat timing tricks on FLTK (needs more documentaion), (see timerTest).


printTable( table, indentLevel )

Usage:



murgaLua.debug.printTable(myTable)




Will navigate the whole table and print ALL of it's contents, which can be pretty useful for debug purposes.


filterTable( path )

Usage:



copyTable = murgaLua.filterTable (myTable, function)




Returns a table containing the data for which the function returns "true" :



myTable =

{

bob = { age = 22, sex = "male", surname = "Smith" },

tracy = { age = 25, sex = "female", surname = "Smith" },

john = { age = 32, sex = "male", surname = "Murga" },

}


copyTable = murgaLua.filterTable (myTable, function (myValue) return myValue.surname == "Smith" end)

murgaLua.debug.printTable(myTable)




The output of which would be :



[tracy] => table

(

[surname] => Smith

[age] => 25

[sex] => female

)

[bob] => table

(

[surname] => Smith

[age] => 22

[sex] => male

)



Courtesy of our other new function debug.printTable (see murgaLuaLib.lua example).