how to display the SimpleXMLElement Object using foreach loop wherein i need to repeat the display with the given numbers of employees from the xml
Here's the php code that i used
<?php
$xml=simplexml_load_file("contacts.xml") or die("Error: Cannot create object");
$test = $xml->employees;
//$test1 = $xml->employees->employee;
foreach($xml->employees->employee as $test1 => $value)
{
print_r($value);
}
?>
Here's the XML sample that i used
<directory>
<fieldset>
<field id="displayName">Display name</field>
<field id="firstName">First name</field>
<field id="lastName">Last name</field>
<field id="nickname">Nick name</field>
<field id="gender">Gender</field><field id="jobTitle">Job title</field>
<field id="workPhone">Work Phone</field>
<field id="mobilePhone">Mobile Phone</field>
<field id="workEmail">Work Email</field>
<field id="department">Department</field>
<field id="location">Location</field>
<field id="workPhoneExtension">Work Ext.</field>
<field id="photoUploaded">Employee photo</field>
<field id="photoUrl">Photo URL</field>
<field id="canUploadPhoto">Can Upload Photo</field>
</fieldset>
<employees>
<employee id="40691">
<field id="displayName">Analyn Adamag</field>
<field id="firstName">Analyn</field>
<field id="lastName">Adamag</field>
<field id="nickname"/>
<field id="gender">Female</field>
<field id="jobTitle">Senior Administrator</field>
<field id="workPhone"/>
<field id="mobilePhone"/>
<field id="workEmail">[email protected]</field>
<field id="department">Admin</field>
<field id="location">Baguio City</field>
<field id="workPhoneExtension"/>
<field id="photoUploaded">true</field>
<field id="photoUrl">https://e965fc140e935b23d489-b59e834ee1767042e7207caa7b0e4eaf.ssl.cf3.rackcdn.com/photos/40691-2-1.jpg</field>
<field id="canUploadPhoto">yes</field>
</employee>
<employee id="40321">
<field id="displayName">RonaAnalyn Adamagewas</field>
<field id="firstName">Analyn</field>
<field id="lastName">Adamag</field>
<field id="nickname"/>
<field id="gender">Female</field>
<field id="jobTitle">Senior Administrator</field>
<field id="workPhone"/>
<field id="mobilePhone"/>
<field id="workEmail">[email protected]</field>
<field id="department">Admin</field>
<field id="location">Baguio City</field>
<field id="workPhoneExtension"/>
<field id="photoUploaded">true</field>
<field id="photoUrl">https://e965fc140e935b23d489-b59e834ee1767042e7207caa7b0e4eaf.ssl.cf3.rackcdn.com/photos/40691-2-1.jpg</field>
<field id="canUploadPhoto">yes</field>
</employee>
</employees>
</directory>
Here's the output generated when i execute the php code above
SimpleXMLElement Object
(
[fieldset] => SimpleXMLElement Object
(
[field] => Array
(
[0] => Display name
[1] => First name
[2] => Last name
[3] => Nick name
[4] => Gender
[5] => Job title
[6] => Work Phone
[7] => Mobile Phone
[8] => Work Email
[9] => Department
[10] => Location
[11] => Work Ext.
[12] => Employee photo
[13] => Photo URL
[14] => Can Upload Photo
)
)
[employees] => SimpleXMLElement Object
(
[employee] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 40691
)
[field] => Array
(
[0] => Analyn Adamag
[1] => Analyn
[2] => Adamag
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => nickname
)
)
[4] => Female
[5] => Senior Administrator
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => workPhone
)
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mobilePhone
)
)
[8] => [email protected]
[9] => Admin
[10] => Baguio City
[11] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => workPhoneExtension
)
)
[12] => true
[13] => https://e965fc140e935b23d489-b59e834ee1767042e7207caa7b0e4eaf.ssl.cf3.rackcdn.com/photos/40691-2-1.jpg
[14] => yes
)
)
)
)
)
foreachand had an issue?