I'm rather stuck on something. Is there a way to read an XML file using the Synchronet Javascript model?
JSON has the lovely JSON.parse(), but I'm not sure how to read the data, or maybe convert it into JSON to make things easier. I even noticed that XMLHttpRequest is not defined, while HTTPRequest is defined.
A lot of the code snippets I find tend to use things in the DOM and has things like hasChildNodes which is not defined in Core JS or Sync.
For reference, I am trying to grab the data found here: http://www.hamqsl.com/solarxml.php
I'm rather stuck on something. Is there a way to read an XML file
using the Synchronet Javascript model?
E4X (ECMAScript for XML, IIRC) is still available in Synchronet's JS interpreter as far as I know. It's kinda shitty but can get the job done. Documentation can be found on the web.
There are probably a few Synchronet-specific examples of how to use it; 'exec/load/rss-atom.js', which I made, is not the best but does what it does and should be easy to follow. It uses E4X to turn an RSS or Atom feed (both are XML) into an easier-to-work-with JS object.
JSON has the lovely JSON.parse(), but I'm not sure how to read the
data, or maybe convert it into JSON to make things easier. I even
noticed that XMLHttpRequest is not defined, while HTTPRequest is
defined.
XMLHttpRequest is something that exists in browsers, and doesn't necessarily have to have anything to do with XML. It's a way for a script to make the browser load something asynchronously / in the background via HTTP; good for updating content on a page without the user having to reload.
HTTPRequest is defined if you load 'exec/load/http.js' into your script. That's an HTTP(S) client for Synchronet's JS environment which you can use in your scripts (and I assume you already do in that weather script).
A lot of the code snippets I find tend to use things in the DOM and
has things like hasChildNodes which is not defined in Core JS or Sync.
Yes, if you look for XML parsing via JS, the vast majority of examples will be DOM-based and/or browser specific. We don't have the DOM around here. It's possible that you'd be able to find a pure-JS parser if you looked, which may or may not be portable for Synchronet with some effort.
For reference, I am trying to grab the data found here:
http://www.hamqsl.com/solarxml.php
There's already some code for dealing with this exact feed, but it's embedded in an IRC bot. You might be able to find something useful in: '/exec/ircbots/ham/ham.js', however this should work:
load('http.js');
try {
var solardata = new XML(
(new HTTPRequest()).Get(
'http://www.hamqsl.com/solarxml.php'
).replace(
/<\?[^?]*\?>/g, ''
)
).solardata;
} catch (err) {
log('Shit done borked! ' + err);
}
(See https://bbs.electronicchicken.com/temp/solar.txt if that didn't come through okay.)
You should then be able to get at the values from the feed like so:
print(solardata.sunspots);
print(solardata.solarwind);
And so on.
The IRC bot code mentioned above has examples of how to deal with the nested 'calculated(vhf)conditions' values, which gets deeper into E4X than I care to do right now.
Hope this helps.
Sysop: | Tandy |
---|---|
Location: | New York, USA |
Users: | 15 |
Nodes: | 13 (0 / 13) |
Uptime: | 04:02:04 |
Calls: | 335 |
Messages: | 112,921 |