jcadima
1/9/2017 - 4:14 AM

Reading XML with SimpleXML

Reading XML with SimpleXML

<?php
//  members.xml:
<members>
	<member id="1" position="president">
		<firstName>Jim</firstName>
		<lastName>Smith</lastName>
		<score>12</score>
	</member>
	<member id="2">
		<firstName>Hank</firstName>
		<lastName>Rogers</lastName>
		<score>18</score>
	</member>
	<member id="3" position="secretary">
		<firstName>Joy</firstName>
		<lastName>Ashton</lastName>
		<score>21</score>
	</member>
</members>


$xml = simplexml_load_file('members.xml');

foreach($xml->member as $member) {

	//build position information
	if(trim($member['position']) <> '')
		$positionInfo = ' (' . $member['position'] . ')';
	else {
		$positionInfo = '';
	}

	echo "$member->firstName $member->lastName$positionInfo got a score of $member->score.<br/>";
}