Logic Machine Forum
XML data - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: XML data (/showthread.php?tid=2521)



XML data - tomnord - 18.03.2020

Hello, I`m looking to import weaterdata from a website.
The site exports data as xml.

Is there a guide to doing so in LM?

Example xml:
https://www.yr.no/sted/Norge/Vestfold_og_Telemark/Midt-Telemark/Gvarv/varsel.xml


Sent fra min SM-G950F via Tapatalk


RE: XML data - Daniel - 18.03.2020

Code:
require('socket.http')

forecast = {}

function parsetag(parser, tag, attributes)
  if tag == 'time' then
    table.insert(forecast, {})
  elseif tag == 'symbol' then
    forecast[ #forecast ].symbol = attributes.name
  elseif tag == 'precipitation' then
    forecast[ #forecast ].precipitation = attributes.value
  elseif tag == 'windDirection' then
    forecast[ #forecast ].wind_direction = attributes.code
  elseif tag == 'windSpeed' then
    forecast[ #forecast ].wind_speed = attributes.mps
  elseif tag == 'temperature' then
    forecast[ #forecast ].temperature = attributes.value
  elseif tag == 'pressure' then
    forecast[ #forecast ].pressure = attributes.value
  end
end

url = 'http://www.yr.no/place/United_Kingdom/England/Farnborough/varsel.xml'
data, err = socket.http.request(url)

if data then
  require('lxp').new({
    StartElement = parsetag
  }):parse(data)


  grp.update('Weather precipitation', tonumber(forecast[1].precipitation))
  grp.update('Weather symbol', forecast[1].symbol)
  grp.update('Weather windDirection', forecast[1].wind_direction)
  grp.update('Weather windSpeed', tonumber(forecast[1].wind_speed))
  grp.update('Weather temperature', tonumber(forecast[1].temperature))
  grp.update('Weather pressure', tonumber(forecast[1].pressure))

  --log(forecast)
 
else
  alert('Fetch failed: ' .. tostring(err))
end



RE: XML data - Tokatubs - 18.03.2020

Daniel, is this still working. ?

I think yr.no changed to another api.

https://api.met.no/


RE: XML data - admin - 18.03.2020

Their new beta API provides JSON which is much easier to parse:
https://api.met.no/weatherapi/locationforecast/2.0/.json?lat=60.10&lon=9.58


RE: XML data - Daniel - 18.03.2020

My script still works.


RE: XML data - Tokatubs - 18.03.2020

require('ssl.https')
require('json')

data = ssl.https.request('http://weerlive.nl/api/json-10min.php?locatie=' ..plaatsnaam)
datatable = json.pdecode(data)
datatable = datatable['liveweer'][1]

log(datatable)

If you use something like this, how do you deploy it and how do you extract this to groupo adresses?


RE: XML data - tomnord - 18.03.2020

(18.03.2020, 09:29)admin Wrote: Their new beta API provides JSON which is much easier to parse:
https://api.met.no/weatherapi/locationforecast/2.0/.json?lat=60.10&lon=9.58
Where do we privide location on this one?

never mind, I see the long lat now Smile

decoding json gives me "nesting to deep" in most of the tables..
What could be the reason for this?


RE: XML data - Daniel - 18.03.2020

I tried like that you have to put coordinate of location as longitude and latitude.
Code:
require('ssl.https')
require('json')

data = ssl.https.request('https://api.met.no/weatherapi/locationforecast/2.0/.json?lat=51.28&lon=-0.75')
datatable = json.pdecode(data)
log(datatable.properties.timeseries[1].data.instant.details.air_temperature)



RE: XML data - tomnord - 18.03.2020

(18.03.2020, 10:22)Daniel. Wrote: I tried like that you have to put coordinate of location as longitude and latitude.
Code:
require('ssl.https')
require('json')

data = ssl.https.request('https://api.met.no/weatherapi/locationforecast/2.0/.json?lat=51.28&lon=-0.75')
datatable = json.pdecode(data)
log(datatable.properties.timeseries[1].data.instant.details.air_temperature)
yeah, I got some data, but I also got "nesting to deep" in the log. could try to extract spesiffic data, maybe that would do the trick.

seems to work, but the amount of data in the table is quite big, and difficult to navigate.
Any good tips to view the table in a structured way?


RE: XML data - admin - 18.03.2020

Firefox can display JSON data in a structured way. Or just copy JSON from the URL and use any online JSON viewer.