Author Topic: trouble parsing an xml file  (Read 304 times)

0 Members and 1 Guest are viewing this topic.

Offline blainec

  • Senior Member
  • **
  • Posts: 70
    • Weather YYC
trouble parsing an xml file
« on: November 17, 2019, 11:21:32 PM »
Code: [Select]
<conditionAirQuality productHeader="AQ_OBS">
<region nameEn="Calgary" nameFr="Calgary">IAKID</region>
<dateStamp name="aqObserved" zoneEn="MST" zoneFr="HNR">
<year>2019</year>
<month nameEn="November" nameFr="novembre">11</month>
<day nameEn="Sunday" nameFr="dimanche">17</day>
<hour clock="12h" ampm="PM">7</hour>
<minute>00</minute>
<second/>
<UTCStamp>20191118020000</UTCStamp>
<textSummary lang="EN">7:00 PM MST Sunday 17 November 2019</textSummary>
<textSummary lang="FR">19h00 HNR dimanche 17 novembre 2019</textSummary>
</dateStamp>
<airQualityHealthIndex>2.1</airQualityHealthIndex>
<associatedStations>
<station napsid="90222" nameEn="Calgary Northwest" nameFr="Calgary - nord-ouest">
<airQualityHealthIndex>1.9</airQualityHealthIndex>
</station>
<station napsid="90228" nameEn="Calgary Central" nameFr="Calgary - centre">
<airQualityHealthIndex>2.2</airQualityHealthIndex>
</station>
<station napsid="90229" nameEn="Calgary Southeast" nameFr="Calgary - sud-est">
<airQualityHealthIndex>2.1</airQualityHealthIndex>
</station>
</associatedStations>
<specialNotes/>
</conditionAirQuality>

I'm trying to get the City Name (Calgary in nameEn).
I can get it using this code;
Code: [Select]
$city=$xml->region[0][nameEn];but it throws a warning of "Warning: Use of undefined constant nameEn - assumed 'nameEn' (this will throw an Error in a future version of PHP) "

Can anyone tell me what the correct way to parse this is?

Thanks

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: trouble parsing an xml file
« Reply #1 on: November 18, 2019, 02:53:48 AM »
I'm trying to get the City Name (Calgary in nameEn).
I can get it using this code;
Code: [Select]
$city=$xml->region[0][nameEn];but it throws a warning of "Warning: Use of undefined constant nameEn - assumed 'nameEn' (this will throw an Error in a future version of PHP) "
Can anyone tell me what the correct way to parse this is?
Thanks
You need to enclose text-strings which address an array-element in quotes. 
Code: [Select]
$city=$xml->region[0]['nameEn'];

Wim
« Last Edit: November 18, 2019, 02:55:36 AM by wvdkuil »