I am new to lua and was working on NodeMCU. I was trying to extract data from an xml file.
Here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<netconfig>
<mode>0</mode>
<stamac>18-FE-34-A4-4B-05</stamac>
<staip>XXX.XXX.XXX.XXX</staip>
<stanetmask>XXX.XXX.XXX.XXX</stanetmask>
<stagateway>XXX.XXX.XXX.XXX</stagateway>
<apmac>1A-FE-34-A4-4B-05</apmac>
<apip>192.168.4.1</apip>
<apnetmask>255.255.255.0</apnetmask>
<apgateway>192.168.4.1</apgateway>
<port>80</port>
<dns>XXX.XXX.XXX.XXX</dns>
<dhcp>1</dhcp>
<stacustomconfig></stacustomconfig>
<timezone>10</timezone>
<serial>0x00000001</serial>
<connssid>ESP-10767109</connssid>
<ssid></ssid>
<passwd></passwd>
<hostname>ESP-10767109</hostname>
<reboot></reboot>
<message></message>
</netconfig>
Here is my xmlparser:
return function (xmlfile, xmlword)
file.open(xmlfile,"r")
local eofflag = 0
local i, j, k, l, xmloutput
while(eofflag < 1) do
local m = file.readline()
if(m == nil) then
eofflag = eofflag + 1
elseif (string.find(m, xmlword) ~= nil) then
i, j = string.find(m, xmlword, 1)
i = i - 1
j = j + 2
k, l = string.find(m, xmlword, j)
k = k - 3
l = l + 1
xmloutput = string.sub(m, j, k)
eoffile = 1
end
end
file.close()
return xmloutput
end
I call this file by using:
local port = dofile("xmlparser.lc")("netconfig.xml", "port")
But I always endup with memory less than I started with even though I call the garbagecollector. Moreover The heap seems to decrease further if the word I am searching for is near the end of file. I also observed that if the word I am looking for is not present, the heap has the less difference I started with.
Am I missing something? and thanks for taking a look.
Alternatively, is the XML strictly for settings/only used by your lua code? If so I found it much easier and less memory and compute intensive to create the settings file in lua syntax, and then simply execute it. Declare a global table and exec the file:
and in the settings.lua file assign members:
You can write the table to file easily:
For what it's worth...