0

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
                                )

                        )
        )

    )
)
3
  • Is that pseudo employee data, might want to edit out email at least.. What do you want to output everything? Have you tried iterating through with foreach and had an issue? Commented Aug 20, 2015 at 1:00
  • hi chris85, thanks for checking. i was able to render out the employees however, im unable to retrieve the values under employee->field[0] etc. the foreach loop is working. Commented Aug 20, 2015 at 1:04
  • Can you post a small sample of the actual XML file?..or are you getting an error or just no output? Commented Aug 20, 2015 at 1:06

1 Answer 1

1

You can use the xpath, http://php.net/manual/en/simplexmlelement.xpath.php, to query for the specific attribute values. For example to get the first names, last names, and display names of your employees you could do...

$xml = '<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>';
$sxml = new SimpleXmlElement($xml);
$displayNames = $sxml->xpath('//employees/employee/field[@id="displayName"]');
$firstNames = $sxml->xpath('//employees/employee/field[@id="firstName"]');
$lastNames = $sxml->xpath('//employees/employee/field[@id="lastName"]');
for($key = 0; $key < count($displayNames); $key++){
    echo $displayNames[$key] . "\n" .
        $firstNames[$key] . ' ' . $lastNames[$key] ."\n\n";
}

Output (command line, for browser change \n to <br>:

Analyn Adamag
Analyn Adamag

RonaAnalyn Adamagewas
Analyn Adamag
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.