如何使用foreach循环显示SimpleXMLElement对象

时间:2015-08-20 00:31:40

标签: php xml

如何使用foreach循环显示SimpleXMLElement对象,其中我需要使用xml中给定数量的雇员重复显示

这是我使用的php代码

      <?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);


    }



  ?>

这是我使用的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">analyn@sample.com</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">analyn@sample.com</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>

这是我执行上面的php代码时生成的输出

    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] => analyn@datatel.com.au
                                    [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
                                )

                        )
        )

    )
)

1 个答案:

答案 0 :(得分:1)

您可以使用xpathhttp://php.net/manual/en/simplexmlelement.xpath.php来查询特定的属性值。例如,要获得您可以做的员工的名字,姓氏和显示名称......

$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">analyn@sample.com</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">analyn@sample.com</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";
}

输出(命令行,用于浏览器更改\n<br>

Analyn Adamag
Analyn Adamag

RonaAnalyn Adamagewas
Analyn Adamag