Parsing XML problem

Hi there,

I have a script which gets a geographic hierarchy from a webservice;

This pulls the hierarchy for a spanish municipality, in this case Valedepenas.

It also gives me the province

http://ws.geonames.org/hierarchy?geonameId=2509983

I want to pull out the geonameId of the province.

The province has the fcode ADM2

How can I do that?

http://ws.geonames.org/hierarchy?geonameId=2509983


<geonames>
&#8722;
<geoname>
<name>Earth</name>
<lat>0.0</lat>
<lng>0.0</lng>
<geonameId>6295630</geonameId>
<countryCode/>
<countryName/>
<fcl>L</fcl>
<fcode/>
</geoname>
&#8722;
<geoname>
<name>Europe</name>
<lat>48.6909603909255</lat>
<lng>9.140625</lng>
<geonameId>6255148</geonameId>
<countryCode/>
<countryName/>
<fcl>L</fcl>
<fcode>CONT</fcode>
</geoname>
&#8722;
<geoname>
<name>Spain</name>
<lat>40.0</lat>
<lng>-4.0</lng>
<geonameId>2510769</geonameId>
<countryCode>ES</countryCode>
<countryName>Spain</countryName>
<fcl>A</fcl>
<fcode>PCLI</fcode>
</geoname>
&#8722;
<geoname>
<name>Castille-La Mancha</name>
<lat>39.5</lat>
<lng>-3.0</lng>
<geonameId>2593111</geonameId>
<countryCode>ES</countryCode>
<countryName>Spain</countryName>
<fcl>A</fcl>
<fcode>ADM1</fcode>
</geoname>
&#8722;
<geoname>
<name>Ciudad Real</name>
<lat>39.0</lat>
<lng>-4.0</lng>
<geonameId>2519401</geonameId>
<countryCode>ES</countryCode>
<countryName>Spain</countryName>
<fcl>A</fcl>
<fcode>ADM2</fcode>
</geoname>
&#8722;
<geoname>
<name>Valdepeñas</name>
<lat>38.7666667</lat>
<lng>-3.3833333</lng>
<geonameId>2509983</geonameId>
<countryCode>ES</countryCode>
<countryName>Spain</countryName>
<fcl>A</fcl>
<fcode>ADM3</fcode>
</geoname>
</geonames>


I should probably have more confidence in my ability

Is there a better way to do this?



$xml = simplexml_load_file($link);
// loop begins
foreach($xml->geoname as $geoname)
{

$fcode = $geoname->fcode;

	if ($fcode=="ADM2") {
	echo $geoname->geonameId.$geoname->name.$geoname->name.'<br>';
	}
}



No, there isn’t a better way to do it. You done it correctly.

However, please remove the “loop begins” comment – it is completely unnecessary. Any other programmer who takes over your code will wish comments like that weren’t there.