How to iterate through XML values that are a Simpl

2019-07-18 03:51发布

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.

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;

This fails to iterate through the available values:

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

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!

1条回答
萌系小妹纸
2楼-- · 2019-07-18 04:33

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>";
    }
}
查看更多
登录 后发表回答