Php 如何迭代作为SimpleXMLElement对象的XML值
我将一个XML流解析为一个SimpleXMLElement对象,并尝试迭代可用的记录以用作PHP页面中的值 [listing]的父节点当前存在两次,因为测试XML中有两条记录(清单[0]和清单[1]) 但是我不能让它像PHP手册中的“基本SimpleXML用法”那样工作Php 如何迭代作为SimpleXMLElement对象的XML值,php,xml,simplexml,Php,Xml,Simplexml,我将一个XML流解析为一个SimpleXMLElement对象,并尝试迭代可用的记录以用作PHP页面中的值 [listing]的父节点当前存在两次,因为测试XML中有两条记录(清单[0]和清单[1]) 但是我不能让它像PHP手册中的“基本SimpleXML用法”那样工作 <?php $xml = simplexml_load_file('http://feed.postlets.com/Burndog/6458ec1af54f632'); 这无法遍历可用值: fo
<?php
$xml = simplexml_load_file('http://feed.postlets.com/Burndog/6458ec1af54f632');
这无法遍历可用值:
foreach ($xml->listing->title as $title) {
echo $title;
}
?>
打印文件中的值:
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
那么,当图片中有多个元素时,需要通过哪些元素进行循环?
谢谢 正如您在打印输出中看到的,XML对象的“listing”字段是数组,而不是标题。因此,您需要做的是遍历列表并打印出每个列表标题:
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>";
}
}
foreach($xml->listing as$listing)
{
回显“标题:”.$listing->Title.“
”;
foreach($listing->photos->children()作为$child)
{
echo$child。“
”;
}
}
您的第一个foreach功能完美。。。第二个渲染:photos=>photos=>我将进一步研究它,因为您的值可能只是表示,我需要插入实际字段值。使用alternative编辑,希望有帮助。ccKep,您的foreach产品的结果:标题:3BR/2BA已生产-博蒙特照片数量:0.5标题:2BR/1+1BA已生产-博蒙特照片数量:0.5不知道您现在是否解决了此问题。如果没有,请尝试上面编辑的代码(下面的代码“可选”)并进行检查。结果:标题:3BR/2BA已制造-博蒙特照片1照片2照片3标题:2BR/1+1BA已制造-博蒙特前视图目标已实现!谢谢,这可能会有帮助:
foreach ($xml->listing as $listing)
{
echo "Title: " . $listing->title . "<br>";
foreach ($listing->photos->children() as $child)
{
echo $child . "<br>";
}
}