Climate Data API - node name confusing php parser

56 views
Skip to first unread message

gaëlle fernandez

unread,
Apr 12, 2013, 1:01:38 PM4/12/13
to world-b...@googlegroups.com
Hello everyone!!

I'm working on a way to draw some chart with climate datas, and i'm only at the first step : parsing the xml given by the api.

What i've already done :
The user select a country and a range of years in a form.
Then, i can select which xml i have to ask the climate API :

http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/".$firstYear."/".$lastYear."/".$countryIso.".xml

ie : http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/1940/1959/FRA.xml

(This xml whose content is copied in the file i give you)

As you can see, the first children of <list> is <domain.web.AnnualGcmDatum>

Then, i use a SimpleXMLElement to parse the xml :

$fichier =  "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/".$firstYear."/".$lastYear."/".$countryIso.".xml";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$fichier);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);  //récupère le contenu sous forme de chaine
$contenu = curl_exec($curl);
$xml = new SimpleXMLElement($contenu);
foreach($xml->domain.web.AnnualGcmDatum as $v){
   echo '<li>'.$v->annualData.'</li>';
 }

BUT, because of the strange name of the node, php does not understand what i want to do.

Maybe i've done something wrong, so please tell me my mistake, this could help me go further.

Thanks by advance


gaëlle fernandez

unread,
Apr 12, 2013, 1:03:53 PM4/12/13
to world-b...@googlegroups.com
I forgot to bring you the datas.xml file... -_-
Sorry
datas.xml

Sarven Capadisli

unread,
Apr 12, 2013, 1:53:58 PM4/12/13
to world-b...@googlegroups.com
On 04/12/2013 01:01 PM, gaëlle fernandez wrote:
> Hello everyone!!
>
> I'm working on a way to draw some chart with climate datas, and i'm only
> at the first step : parsing the xml given by the api.
>
> *What i've already done :*
> The user select a country and a range of years in a form.
> Then, i can select which xml i have to ask the climate API :
>
> http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/".$firstYear."/".$lastYear."/".$countryIso.".xml
>
> ie :
> http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/1940/1959/FRA.xml
>
> /(This xml whose content is copied in the file i give you)/
>
> *As you can see, the first children of <list> is
> <domain.web.AnnualGcmDatum>*
>
> Then, i use a SimpleXMLElement to parse the xml :
>
> $fichier =
> "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/".$firstYear."/".$lastYear."/".$countryIso.".xml";
> $curl = curl_init();
> curl_setopt($curl,CURLOPT_URL,$fichier);
> curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); //récupère le
> contenu sous forme de chaine
> $contenu = curl_exec($curl);
> $xml = new SimpleXMLElement($contenu);
> foreach($xml->domain.web.AnnualGcmDatum as $v){
> echo '<li>'.$v->annualData.'</li>';
> }
>
>
> *BUT*, because of the strange name of the node, php does not understand
> what i want to do.
>
> Maybe i've done something wrong, so please tell me my mistake, this
> could help me go further.
>
> Thanks by advance
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "World Bank API" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to world-bank-ap...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


Hi Gaëlle,

There is nothing strange or wrong about the node name (see [1]).

What you have to resolve is proper way to access XML nodes with the PHP
library that you are using. I may be wrong, but I think that's outside
of the scope of this mailing list. I would however suggest that use use
SimpleXMLElement::xpath or something like
$xml->{'domain.web.AnnualGcmDatum'}.

[1] http://www.w3.org/TR/REC-xml/#sec-common-syn

-Sarven
http://csarven.ca/#i

gaëlle fernandez

unread,
Apr 12, 2013, 4:33:24 PM4/12/13
to world-b...@googlegroups.com
Hi Sarven,

Many thanks to you to help me, I'll try the way you say and read the w3C documentation to see where i'm wrong.
And...many sorry if my subject wasn't in the right place!
I'll come back to you after trying what you say, if it does not bother you..

Good night,

Gaëlle


2013/4/12 Sarven Capadisli <in...@csarven.ca>

For more options, visit https://groups.google.com/groups/opt_out.




Hi Gaëlle,

There is nothing strange or wrong about the node name (see [1]).

What you have to resolve is proper way to access XML nodes with the PHP library that you are using. I may be wrong, but I think that's outside of the scope of this mailing list. I would however suggest that use use SimpleXMLElement::xpath or something like $xml->{'domain.web.AnnualGcmDatum'}.

[1] http://www.w3.org/TR/REC-xml/#sec-common-syn

-Sarven
http://csarven.ca/#i




--

Gaëlle FERNANDEZ,
Developpeur PHP junior
06 - 78 - 55 - 07 - 63

gaëlle fernandez

unread,
Apr 15, 2013, 8:59:11 AM4/15/13
to world-b...@googlegroups.com
Well, thanks you Sarven, i tried with xpath, and after some minutes working on it, i can read the informations i wanted in the first place!
So, mea culpa, there is nothing wrong with the node name, just me not used to xml files.
The SimpleXMLElement::xpath works really great.

have a nice week everybody



2013/4/12 gaëlle fernandez <gaelle.f...@gmail.com>
Reply all
Reply to author
Forward
0 new messages