作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个解析为 SimpleXMLElement 对象的 XML 流,我正在尝试遍历可用记录以用作 PHP 页面中的值。
[listing]的父节点目前存在两次,因为测试XML中有两条记录(listing[0]和listing[1])但是我无法像 PHP 手册中的“基本 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
那么在图片元素不止一个的情况下,如何循环遍历图片元素呢?谢谢!
最佳答案
正如您在 print_r 输出中看到的,XML 对象的“列表”字段是数组,而不是标题。因此,您需要做的是遍历列表并打印出每个列表标题:
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>";
}
}
关于php - 如何遍历作为 SimpleXMLElement 对象的 XML 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9321148/
我是一名优秀的程序员,十分优秀!