To be able to parse that document (which is not well formed) I would recommend to do the following:
$xmlString = file_get_contents('rapleaf.xml');
$xmlString = str_replace('&', '&', $xmlString);
if(!$xml=simplexml_load_string($xmlString)){
trigger_error('Error reading XML file',E_USER_ERROR);
}
First read the file into a string, that replace the ampersand characters (within the link) with their entity. That you can use the simplexml_load_file() function to create the xml object.
Now you are able to parse the document. As far as I can see, there is only one person in each file. So you don't need a foreach loop. But you can parse all field, you just have to know how. Here is some more complex exmaple parsing different things with different methods:
echo ' Name: '.(string)$xml->basics->name. '
<br /> Age: '.(string)$xml->basics->age.'
<br /> gender: '.(string)$xml->basics->gender.'
<br /> Address: '.(string)$xml->basics->location;
// There might be more than one occupation
foreach($xml->occupations as $occupation){
echo '<br /> Occupation: '.$occupation->attributes()->title;
if(isset($occupation->attributes()->company)){
echo '; at company: '.$occupation->attributes()->company;
}
}
// There might be more than one university
foreach($xml->universities as $university){
echo '<br /> University: '.$university;
}
echo '<br /> first seen: '.(string)$xml->basics->earliest_known_activity.'
<br /> last seen: '.(string)$xml->basics->latest_known_activity.'
<br /> Friends: '.(string)$xml->basics->num_friends;
// getting all the primary membership pages
foreach($xml->memberships->primary->membership as $membership){
if($membership->attributes()->exists == "true"){
echo '<br />'.$membership->attributes()->site;
if(isset($membership->attributes()->profile_url)){
echo ' | '.$membership->attributes()->profile_url;
}
if(isset($membership->attributes()->num_friends)){
echo ' | '.$membership->attributes()->num_friends;
}
}
}
For Text that is included in a tag, you have to cast it to string:
echo 'Name: '.(string)$xml->basics->name;
To get the value of an attribute of a tag, use the attributes() function. You don't have to cast it this time:
echo 'Occupation: '.$xml->occupations->occupation[0]->attributes()->title;
As you can see, you can also get a specific child node, as all the child nodes are stored in an array. Just use the index. If you just want one child node, you don't have to use a loop for that.
But you always have to make sure that the element you are using the attirbutes() function on is valid as otherwise an error will be thrown. So so may want to test that via isset() to be sure.
I hop you now have an idea on how to parse some XML using SimpleXML. If you have any additional questions, just ask again or even in a new question.