如何迭代作为SimpleXMLElement对象的XML值

时间:2012-02-17 00:14:35

标签: php xml simplexml

我有一个解析为SimpleXMLElement对象的XML流,我试图迭代可用的记录以用作PHP页面中的值。

[listing]的父节点当前存在两次,因为测试XML中有两条记录(列出[0]和列表[1]) 但我不能像PHP手册中的“Basic SimpleXML用法”所示那样工作

    <?php
    $xml = simplexml_load_file('http://feed.postlets.com/Burndog/6458ec1af54f632');

这可以提供第一个列表标题元素值:

    $value1 = $xml->listing[0]->title;
    echo ' here:' . $value1;

无法迭代可用值:

    foreach ($xml->listing->title as $title) {
    echo $title;
    }
    ?>
来自print_r的

值:

SimpleXMLElement Object
(
[listing] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [url] => http://www.postlets.com/repb/6509636
                [title] => 3BR/2BA Manufactured - Beaumont
                [subtitle] => SimpleXMLElement Object
                    (
                    )

                [description] => SimpleXMLElement Object
                    (
                    )

                [location] => SimpleXMLElement Object
                    (
                        [street] => 1415 E 6th St
                        [city] => Beaumont
                        [zipcode] => 92223
                        [state] => CA
                        [latitude] => 33.928326
                        [longitude] => -116.959923
                        [walkscore] => 46
                    )

                [details] => SimpleXMLElement Object
                    (
                        [money] => SimpleXMLElement Object
                            (
                                [price] => 44900
                            )

                        [property_for] => Sale
                        [property_use] => Residential
                        [property_type] => Manufactured
                        [year_built] => 2011
                        [bedrooms] => 3
                        [full_bathrooms] => 2
                        [partial_bathrooms] => 0
                        [sqft] => 1041
                        [lot_size] => 1045 sqft
                        [parking] => SimpleXMLElement Object
                            (
                            )

                    )

                [photos] => SimpleXMLElement Object
                    (
                        [photo_1] => http://www.postlets.com/create/photos/20111101/082821_6509636_158803034.jpg
                        [photo_caption_1] => Photo 1
                        [photo_2] => http://www.postlets.com/create/photos/20111101/082822_6509636_3416721218.jpg
                        [photo_caption_2] => Photo 2
                        [photo_3] => http://www.postlets.com/create/photos/20111101/082822_6509636_1298858591.jpg
                        [photo_caption_3] => Photo 3
                    )

                [contact] => SimpleXMLElement Object
                    (

                    )

            )

        [1] => SimpleXMLElement Object
            (
                [url] => http://www.postlets.com/repb/7066849
                [title] => 2BR/1+1BA Manufactured - Beaumont
                [subtitle] => SimpleXMLElement Object
                    (
                    )

                [description] => SimpleXMLElement Object
                    (
                    )

                [location] => SimpleXMLElement Object
                    (
                        [street] => 1415 E 6th St # 12
                        [city] => Beaumont
                        [zipcode] => 92223
                        [state] => CA
                        [latitude] => 33.929199
                        [longitude] => -116.959831
                        [walkscore] => 46
                    )

                [details] => SimpleXMLElement Object
                    (
                        [money] => SimpleXMLElement Object
                            (
                                [price] => 56000
                                [hoa] => 400
                            )

                        [property_for] => Sale
                        [property_use] => Residential
                        [property_type] => Manufactured
                        [year_built] => 1997
                        [bedrooms] => 2
                        [full_bathrooms] => 1
                        [partial_bathrooms] => 1
                        [sqft] => 1250
                        [lot_size] => 3000 sqft
                        [property_features] => Central A/C, Dining room, Breakfast nook, Dryer
                        [community_features] => Covered parking
                        [parking] => SimpleXMLElement Object
                            (
                            )

                    ) etc etc

然后,如果不止一个,循环显示图片元素需要什么? 谢谢!

1 个答案:

答案 0 :(得分:5)

正如您在print_r输出中看到的那样,&#39;列表&#39; XML-Object的字段是数组,而不是标题。所以你要做的就是遍历列表并打印出每个列表标题:

foreach ($xml->listing as $listing)
{
    echo $listing->title;
}

要打印图片,您可以执行以下操作:

foreach ($xml->listing as $listing)
{
    echo "Title: " . $listing->title . "<br>";

    foreach ($listing->photos->children() as $child)
    {
        echo $child . "<br>";
    }
}