Subj : Synchronet Javascript and reading XML
To : echicken
From : KenDB3
Date : Thu Jul 28 2016 09:11 am
Re: Synchronet Javascript and reading XML
By: echicken to KenDB3 on Wed Jul 27 2016 11:10 pm
Thank you EC, this helps tremendously. It's hard to know what you don't know
sometimes.
>> I'm rather stuck on something. Is there a way to read an XML file
>> using the Synchronet Javascript model?
ec> E4X (ECMAScript for XML, IIRC) is still available in Synchronet's JS
ec> interpreter as far as I know. It's kinda shitty but can get the job done.
ec> Documentation can be found on the web.
ec> There are probably a few Synchronet-specific examples of how to use it;
ec> 'exec/load/rss-atom.js', which I made, is not the best but does what it
ec> does and should be easy to follow. It uses E4X to turn an RSS or Atom feed
ec> (both are XML) into an easier-to-work-with JS object.
I will definitely give that a look. I was trying to think of what might deal
with XML and for the life of me couldn't remember this one.
>> 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.
ec> XMLHttpRequest is something that exists in browsers, and doesn't
ec> necessarily have to have anything to do with XML. It's a way for a script
ec> to make the browser load something asynchronously / in the background via
ec> HTTP; good for updating content on a page without the user having to
ec> reload.
Ahhhh! Gotcha.
ec> HTTPRequest is defined if you load 'exec/load/http.js' into your script.
ec> That's an HTTP(S) client for Synchronet's JS environment which you can use
ec> in your scripts (and I assume you already do in that weather script).
I do use it there, and now, thanks to the explanation, I get the concept better
than I did before. Much appreciated :-)
>> 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.
ec> Yes, if you look for XML parsing via JS, the vast majority of examples
ec> will be DOM-based and/or browser specific. We don't have the DOM around
ec> here. It's possible that you'd be able to find a pure-JS parser if you
ec> looked, which may or may not be portable for Synchronet with some effort.
I did a bunch of looking and at first I wasn't turning up much, but eventually
I found a pure-JS that would do XML to JSON, but the output wasn't the best. It
can be found here for anyone else interested though:
http://www.thomasfrank.se/xml_to_json.html
>> For reference, I am trying to grab the data found here:
>>
http://www.hamqsl.com/solarxml.php
ec> There's already some code for dealing with this exact feed, but it's
ec> embedded in an IRC bot. You might be able to find something useful in:
ec> '/exec/ircbots/ham/ham.js', however this should work:
ec> load('http.js');
ec> try {
ec> var solardata = new XML(
ec> (new HTTPRequest()).Get(
ec> '
http://www.hamqsl.com/solarxml.php'
ec> ).replace(
ec> /<\?[^?]*\?>/g, ''
ec> )
ec> ).solardata;
ec> } catch (err) {
ec> log('Shit done borked! ' + err);
ec> }
ec> (See
https://bbs.electronicchicken.com/temp/solar.txt if that didn't come
ec> through okay.)
ec> You should then be able to get at the values from the feed like so:
ec> print(solardata.sunspots);
ec> print(solardata.solarwind);
ec> And so on.
That's awesome. I really appreciate the help!
ec> The IRC bot code mentioned above has examples of how to deal with the
ec> nested 'calculated(vhf)conditions' values, which gets deeper into E4X than
ec> I care to do right now.
Understood!
ec> Hope this helps.
Immensely. Thanks again for your help EC. Not sure if I can do what I am
setting out to do, but it's always fun playing around with stuff.
~KenDB3