Loading the xml2lua Library to Parse XML Data

Discussion in 'C-Bus Automation Controllers' started by kiwi64ajs, Sep 9, 2025 at 11:42 AM.

  1. kiwi64ajs

    kiwi64ajs

    Joined:
    Nov 15, 2008
    Messages:
    36
    Likes Received:
    0
    Hi Guys,

    Has anyone tried to parse XML data from an external device in a 5500AC2 using the xml2lua library?

    I'm trying to figure out how to extract Dallas 1-Wire Temperature Sensor data from an XML data structure returned from the 1-Wire Gateway device I recently purchased. The gateway scans the 1-wire networks (up to 3) for sensors and then returns the data in a single XML structure, which seems to work well.

    So far, I have been experimenting with writing Lua code on my MacBook using the ZeroBrane Studio IDE and the xml2lua package, and I am making progress.

    Now, the challenge is to parse the data in the Lua code of my 5500AC2, but I need to load the xml2lua package somehow, which has multiple files and subfolders, and it's not apparent how to do it.

    Any suggestions or alternative solutions to the problem of parsing XML data?

    Regards
    Alex Shepherd
     
    kiwi64ajs, Sep 9, 2025 at 11:42 AM
    #1
  2. kiwi64ajs

    SgrAystar

    Joined:
    Oct 4, 2018
    Messages:
    69
    Likes Received:
    5
    Location:
    Melbourne, Australia
    I use the following script to download & parse xml, found at the lm forum.
    Another simpler example https://forum.logicmachine.net/showthread.php?tid=5439&highlight=parse+xml

    Code:
    --[[
    Download the BoM forecast for Melbourne
    --]]
    
    local logYesNo = false
    
    -- Product ID from FTP Public Products and wanted location within the BoM file
    local bomProduct = "IDV10450"
    local location = "Melbourne"
    
    function starttag(p, tag, attrs)
      if tag == 'area' and attrs.type == 'location' and attrs.description == location then
        in_area = true
      elseif tag == 'forecast-period' and in_area then
        index = tonumber(attrs.index)
    
        if index then
          forecast[index] = {}
        end
      elseif index then
        prop = attrs.type
      end
    end
    
    function endtag(p, tag)
      if tag == 'area' then
        in_area = false
      elseif tag == 'forecast-period' then
        index = nil
      elseif index then
        prop = nil
      end
    end
    
    function text(p, txt)
      if in_area and index and prop then
        forecast[index][prop] = txt:trim()
      end
    end
    
    -- download the bom forecast file
    local uri = "ftp://ftp.bom.gov.au/anon/gen/fwo/"..bomProduct..".xml"
    require('socket.ftp')
    local data, err = socket.ftp.get(uri)
    
    if data and not err then
      forecast = {}
    
        require('lxp').new({
          StartElement = starttag,
          EndElement = endtag,
          CharacterData = text,
        }):parse(data)
    
        logger(logYesNo, 'Forecast tree', forecast)
     
      if forecast[0] then
        -- the forecast max for today is not always present in the file
        if forecast[0].air_temperature_maximum then
          local forecastTempMax = tonumber(forecast[0].air_temperature_maximum)
                -- do stuff
        end
      else
        logger(logYesNo, 'Forecast not found in data', data)
      end
    else
      logger(logYesNo, 'No data from '..uri, err)
    end
    
     
    SgrAystar, Sep 9, 2025 at 11:33 PM
    #2
  3. kiwi64ajs

    kiwi64ajs

    Joined:
    Nov 15, 2008
    Messages:
    36
    Likes Received:
    0
    Ah huh - thanks @SgrAystar.

    So is the Lua "lxp" package already installed and part of the Lua environment in the 5500AC2?
     
    kiwi64ajs, Sep 10, 2025 at 12:51 AM
    #3
  4. kiwi64ajs

    SgrAystar

    Joined:
    Oct 4, 2018
    Messages:
    69
    Likes Received:
    5
    Location:
    Melbourne, Australia
    Yes. My 5500AC2 is currently on firmware 2.1.0 but this script has been running for a couple of years on earler releases.
     
    SgrAystar, Sep 10, 2025 at 1:31 AM
    #4
  5. kiwi64ajs

    kiwi64ajs

    Joined:
    Nov 15, 2008
    Messages:
    36
    Likes Received:
    0
    Awesome, thanks for the assistance.
    My requirement should be able to be done using a similar approach.

    Alex
     
    kiwi64ajs, Sep 10, 2025 at 1:49 AM
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.