使用PHP解析XML并在浏览器中显示结果

时间:2013-09-19 13:37:20

标签: php xml

我正在处理一个xml文件,我正在尝试在php中解析并在浏览器上显示它...这里有一些我的代码

<?php

$myurl = "http://localhost/masterclass/xml/REA_2013-09-18-13_31_48.xml";

$curl = curl_init($myurl);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
$result = curl_exec($curl);
$xmldata = simplexml_load_string($result);
foreach ($xmldata->residential->objects->img as $img){
    $attrib = $img->attributes();

    var_dump($attrib['url']);
}
?>

当我运行上述内容时,我得到以下结果..

object(SimpleXMLElement)#16 (1) { [0]=> string(66) "http://assets.boxdice.com.au /one_agency/listings/2912/9e605516.jpg" } object(SimpleXMLElement)#15 (1) { [0]=>   string(66) "http://assets.boxdice.com.au/one_agency/listings/2912/5cdc8604.jpg" }   object(SimpleXMLElement)#14 (1) { [0]=> string(66) "http://assets.boxdice.com.au /one_agency "http://assets.boxdice.com.au/one_agency/listings/2912/45e59869.jpg" }  object(SimpleXMLElement)#16 (1) { [0]=> string(66) "http://assets.boxdice.com.au  /one_agency /listings/2912/aca74229.jpg" } NULL NULL NULL NULL NULL NULL NULL NULL NULL  NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

我需要帮助来提取url并停止循环返回NULL

这是xml文件

<propertyList username="oneagen0" date="2013-09-18-13:01:14" password="maryanne">
<residential status="current" modTime="2013-09-18-13:01:14">
<priceView>
  <![CDATA[Offers Over $535,000]]>
</priceView>
<listingAgent id="1">
<name>Anita Ellis-Case</name>
<email>anitaec@oneagency.com.au</email>
<telephone type="BH">0243812004</telephone>
<telephone type="mobile">0412996488</telephone>
</listingAgent>
<address display="yes">
<streetNumber>145</streetNumber>
<street>Del Monte Place</street>
<suburb display="yes">COPACABANA</suburb>
<state>NSW</state>
<postcode>2251</postcode>
<country>Australia</country>
</address>
<objects>
<img format="jpg" url="http://assets.boxdice.com.au/one_agency/listings /2912/9e605516.jpg" id="m" modTime="2013-09-18-13:01:14"/>
<img format="jpg" url="http://assets.boxdice.com.au/one_agency/listings /2912/5cdc8604.jpg" id="a" modTime="2013-09-18-13:01:14"/>
<img format="jpg" url="http://assets.boxdice.com.au/one_agency/listings/2912/e0374768.jpg" id="b" modTime="2013-09-18-13:01:14"/>
<img format="jpg" url="http://assets.boxdice.com.au/one_agency/listings/2912/45e59869.jpg" id="c" modTime="2013-09-18-13:01:14"/>
<img format="jpg" url="http://assets.boxdice.com.au/one_agency/listings/2912/aca74229.jpg" id="d" modTime="2013-09-18-13:01:14"/>
<img id="e"/>
<img id="f"/>
<img id="g"/>
<img id="h"/>
<img id="i"/>
<img id="j"/>
<img id="k"/>
<img id="l"/>
<img id="n"/>
<img id="o"/>

<img id="p"/>
<img id="q"/>
<img id="r"/>
<img id="s"/>
<img id="t"/>
<img id="u"/>
<img id="v"/>
<img id="w"/>
<img id="x"/>
<img id="y"/>
<img id="z"/>
<floorplan id="1"/>
<floorplan id="2"/>
</objects>
<agentID>ELLISCASE01</agentID>
<uniqueID>OneAgency_1_2912</uniqueID>
<headline>Oceanfront headland position- 600m to the sand</headline>
<description>
<![CDATA[Being offered for sale for the first time, this cliff top property offers   one   of the last remaining opportunities to knock down/ rebuild in this exclusive set of   properties.
 Remaining in the one family for many years and used as a weekender, the owner is   giving one lucky buyer the opportunity to make their own happy memories.
-Three standard bedrooms, main with built in robes
-Central bathroom with bath for children and separate WC
-Dining area off kitchen with back deck access and to rear reserve
-Downstairs poses flexible uses- either bedroom with ensuite or at home office
-Undercover parking
-Private position with valley views to the front
-Ocean views from the rear, walk to the beach and look out]]>
</description>
<authority value="exclusive"/>
<category name="House"/>
<underOffer value="no"/>
<price display="yes">475000.0</price>
<features>
<bathrooms>2</bathrooms>
<bedrooms>4</bedrooms>
<carports>1</carports>
<ensuite>0</ensuite>
<remoteGarage>0</remoteGarage>
<secureParking>0</secureParking>
<airConditioning>0</airConditioning>
<alarmSystem>0</alarmSystem>
<vacuumSystem>0</vacuumSystem>
<intercom>0</intercom>
<poolInGround>0</poolInGround>
<poolAboveGround>0</poolAboveGround>
<tennisCourt>0</tennisCourt>
<balcony>1</balcony>
<deck>0</deck>
<courtyard>0</courtyard>
<outdoorEnt>0</outdoorEnt>
<shed>0</shed>
<fullyFenced>0</fullyFenced>
<openFirePlace>0</openFirePlace>
<toilets>0</toilets>
<livingAreas>0</livingAreas>
<insideSpa>0</insideSpa>
<outsideSpa>0</outsideSpa>
<broadband>0</broadband>
<builtInRobes>0</builtInRobes>
<dishwasher>0</dishwasher>
<ductedCooling>0</ductedCooling>
<ductedHeating>0</ductedHeating>
<evaporativeCooling>0</evaporativeCooling>
<floorboards>0</floorboards>
<gasHeating>0</gasHeating>
<gym>0</gym>
<hydronicHeating>0</hydronicHeating>
<payTV>0</payTV>
<reverseCycleAirCon>0</reverseCycleAirCon>
<rumpusRoom>0</rumpusRoom>
<splitSystemAirCon>0</splitSystemAirCon>
<splitSystemHeating>0</splitSystemHeating>
<study>0</study>
<workshop>0</workshop>
<otherFeatures>Balcony</otherFeatures>
</features>
<landDetails>
<area of="total" unit="squareMeter">746.0</area>
</landDetails>
<videoLink href="http://animoto.com/play/Cd5Q7zQmFbAlfRwDN4DTeQ"/>
</residential>
</propertyList>

1 个答案:

答案 0 :(得分:0)

<img id="e"/>
<img id="f"/>
<img id="g"/>
<img id="h"/>
<img id="i"/>
<img id="j"/>
<img id="k"/>
<img id="l"/>
<img id="n"/>
<img id="o"/>

所有这些都没有任何url属性,因此返回NULL。