Extract strings from online XML

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Stranger
Posts: 2
Joined: April 25th, 2011, 4:17 am
Location: Paris, France
Contact:

Hi Folks,

I'm currently working in order to catch some online datas via an RSS feed. I use a Socket connection for this, which is working fine. 8)

For example, here is the kind of datas I get in the RSS (it's a weather feed):

Code: Select all

<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> 
<rss version="2.0" xmlns:meteo="http://www.meteorologic.net/rss/1.0"> 
<channel><item> 
<meteo:weather date="26 Avril" tempe_matin="10.4" namepictos_matin="Dégagé"   pictos_matin="soleil" tempe_midi="18" namepictos_midi="Dégagé" pictos_midi="soleil" tempe_apmidi="21" namepictos_apmidi="Dégagé"   pictos_apmidi="soleil" tempe_soir="16.4" namepictos_soir="Dégagé"   pictos_soir="soleil" /> 
</item></channel></rss>
Please note that there are other "<meteo:weather" sections in the actual feed, the preview here is simplified. I'm attempting to get the second one, so I presume it would be indexed as "1" ("0" being the first, as always).
Let's assume I'm interested in getting the values into "tempe_matin". My code looks like this:

Code: Select all

// Creating an XML object from the String "xmlFinalString" got via Socket
try
	{
        var rss = new XML (xmlFinalString);
	} catch(e) {
        alert ("Impossible de formater les données recueillies en balises XML.\r " + e.toString());
        break;
	}
	
	var ns = new Namespace ("meteo:weather");
    
        var tempmatin = rss.ns[1].@tempe_matin.toString(); // Here ExtendScript crashes
	
	alert (tempmatin);
I've tried various options, using Namespace or not (anyway, I confess I'm not sure to understand perfectly what a Namespace is... :oops:). Adobe's documentation just let me into the fog.
Could someone explain me my mistake(s) and how to extract a determined string from my XML object?

Thanks a lot for your help,
Julien

PS : Although my English may not be perfect, I hope it remains understandable :wink:
Julien
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

This is not the nicest way to write it, but you could retrieve the tempe_matin attribute of the second meteoweather like this:

Code: Select all

var tempmatin = rss.elements()[0].elements()[0].elements()[1].@tempe_matin;
micpen
Posts: 7
Joined: May 15th, 2012, 1:54 am

Hi Julien,

in fact you are in the wrong namespace.

for -> <rss version="2.0" xmlns:meteo="http://www.meteorologic.net/rss/1.0">

and -> <meteo:weather date="18 Mai" link="http://www.meteorologic.net/meteo-franc ... 29591.html" tempe_matin="11.9" namepictos_matin="Nuageux" pictos_matin="nuageux" tempe_midi="13.8" namepictos_midi="Nuageux" pictos_midi="nuageux" tempe_apmidi="16.6" namepictos_apmidi="Nuageux" pictos_apmidi="nuageux" tempe_soir="15.5" namepictos_soir="Nuageux" pictos_soir="nuageux" />

try :

var ns = new Namespace ("http://www.meteorologic.net/rss/1.0");

var tempmatin = rss.channel.item.ns::weather.@tempe_matin[0];
//var tempmatin = rss.channel[0].item[0].ns::weather.@tempe_matin[0];

alert(tempmatin);

the alert displays 11,9 for me.

Michel
Post Reply