如何遍历作为 SimpleXMLElement 对象的 XML 值 [英] How to iterate through XML values that are a SimpleXMLElement Object

查看:28
本文介绍了如何遍历作为 SimpleXMLElement 对象的 XML 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 流解析为 SimpleXMLElement 对象,我正在尝试遍历可用记录以用作 PHP 页面中的值.

I have an XML stream parsed to a SimpleXMLElement Object and I am trying to iterate though the available records to use as values in a PHP page.

[listing] 的父节点当前存在两次,因为测试 XML 中有两条记录(listing[0] 和 listing[1])但是我无法像 PHP 手册中的Basic SimpleXML 用法"中显示的那样工作

The parent node of [listing] currently exists twice as there are two records in the test XML (listing[0] and listing[1]) But I can not get this to work like shown on the "Basic SimpleXML usage" from the PHP Manual

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

这用于提供第一个列表标题元素值:

This works to provide the first listing title element value:

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

这无法遍历可用值:

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

来自 print_r 的值:

values from a 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

那么,由于图片的元素不止一个,循环遍历需要什么?谢谢!

Then what will it take to loop through the elements for pictures as there is more than one? Thanks!

推荐答案

正如您在 print_r 输出中所见,XML 对象的列表"字段是数组,而不是标题.所以你需要做的是遍历列表并打印出每个列表的标题:

As you can see in your print_r output, the 'listing' field of the XML-Object is the array, not the title. So what you have to do is iterate through the listings and print out each listings title:

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

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

To print out the pictures you'd do something like this:

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

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

这篇关于如何遍历作为 SimpleXMLElement 对象的 XML 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆