我试图从下面的xml提要中获取一些值
<?xml version="1.0" ?>
<SEARCH>
<LOCATION>
<NAME>Terrance</NAME>
<COUNTRY>USA</COUNTRY>
</LOCATION>
<FOUND>
<TOTALOFOUND>
<TOTAL>3</TOTAL>
</TOTALOFOUND>
<PLACE>
<ADDRESS>IL Road</ADDRESS>
<NAME>shop1</NAME>
<POSTCODE>5</POSTCODE>
<CATIM>
<SMALL>ILR.jpg</SMALL>
<MEDIUM>ILR1.jpg</MEDIUM>
<DESCRIPTION>feeds</DESCRIPTION>
</CATIM>
<BUILD>this is the first test xml feed</BUILD>
<ID>1235</ID>
<DIST>
<LAT>25</LAT>
<LONG>547</LONG>
</DIST>
</PLACE>
<PLACE>
<ADDRESS>Peter Road</ADDRESS>
<NAME>textas</NAME>
<POSTCODE>987</POSTCODE>
<CATIM>
<SMALL>test.jpg</SMALL>
<MEDIUM>test1.jpg</MEDIUM>
<DESCRIPTION>feeds new</DESCRIPTION>
</CATIM>
<BUILD>this is the second test xml feed</BUILD>
<ID>1235</ID>
<DIST>
<LAT>25</LAT>
<LONG>547</LONG>
</DIST>
</PLACE>
<PLACE>
<ADDRESS>Thsi is the 3rd st</ADDRESS>
<NAME>utah</NAME>
<POSTCODE>9117</POSTCODE>
<CATIM>
<SMALL>utah.jpg</SMALL>
<MEDIUM>utah1.jpg</MEDIUM>
<DESCRIPTION>feeds new 3</DESCRIPTION>
</CATIM>
<BUILD>this is the third test xml feed</BUILD>
<ID>000000</ID>
<DIST>
<LAT>000</LAT>
<LONG>54000</LONG>
</DIST>
</PLACE>
</FOUND>
</SEARCH>
我使用以下代码来获取值
<?php
strings = file_get_contents("feed.xml");
$xml=simplexml_load_string($strings);
foreach ($xml as $place)
{
echo "Total : ".$place->TOTALOFOUND->TOTAL."<br />";
echo "address: ".$place->PLACE->ADDRESS."<br />";
echo "Name : ".$place->PLACE->NAME."<br />";
echo "post code: ".$place->PLACE->POSTCODE."<br />";
echo "Small image: ".$place->PLACE->CATIM->SMALL."<br />";
echo "Medium Image: ".$place->PLACE->CATIM->MEDIUM."<br />";
echo "Descripton: ".$place->PLACE->CATIM->DESCRIPTION."<br />";
echo "Office ID: ".$place->PLACE->ID."<br />";
echo "Cord Lat: ".$place->PLACE->DIST->LAT."<br />";
echo "Cord Long: ".$place->PLACE->DIST->LONG."<br />"."<br />";
}
?>
问题是虽然我在xml中有3条记录但它只显示了第一个结果。并在开始时给出以下错误。有人可以帮助我吗
Total :
address:
Name :
post code:
Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 16
Small image:
Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 17
Medium Image:
Descripton:
Office ID:
Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 20
Cord Lat:
Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 21
Cord Long:
Total : 3
address: IL Road
Name : shop1
post code: 5
Small image: ILR.jpg
Medium Image: ILR1.jpg
Descripton:
Office ID: 1235
Cord Lat: 25
Cord Long: 547
任何帮助将不胜感激
感谢
答案 0 :(得分:3)
您必须更仔细地查看XML的结构,并根据其层次结构以适当的方式访问节点。
echo "Total : ".$xml->FOUND->TOTALOFOUND->TOTAL."<br />";
foreach ( $xml->FOUND->PLACE as $place )
{
echo "address: ".$place->ADDRESS."<br />";
echo "Name : ".$place->NAME."<br />";
echo "post code: ".$place->POSTCODE."<br />";
echo "Small image: ".$place->CATIM->SMALL."<br />";
echo "Medium Image: ".$place->CATIM->MEDIUM."<br />";
echo "Descripton: ".$place->CATIM->DESCRIPTION."<br />";
echo "Office ID: ".$place->ID."<br />";
echo "Cord Lat: ".$place->DIST->LAT."<br />";
echo "Cord Long: ".$place->DIST->LONG."<br />"."<br />";
}
答案 1 :(得分:0)
这是另一个例子,为什么人们应该总是在他们代表的节点之后命名他们的PHP变量。这样,您就不会迷失在树的层次结构中。
根节点为<SEARCH/>
,因此其PHP var应为$SEARCH
。另外,请注意simplexml_load_file()
$SEARCH = simplexml_load_file('feed.xml');
echo "Total : ".$SEARCH->FOUND->TOTALOFOUND->TOTAL."<br />";
foreach ($SEARCH->FOUND->PLACE as $PLACE)
{
echo "address: ".$PLACE->ADDRESS."<br />";
// etc...
echo "Cord Lat: ".$PLACE->DIST->LAT."<br />";
}