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

使用PHP解析XML并在浏览器中显示结果,php,xml,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, tr

我正在处理一个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']);
}
?>
我需要帮助来提取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>

安妮塔·埃利斯案
anitaec@oneagency.com.au
0243812004
0412996488
145
德尔蒙特广场酒店
科帕卡巴纳
新南威尔士州
2251
澳大利亚
ELLISCASE01
OneAgency_1_2912
海滨岬角位置-距离沙滩600米
475000
2.
4.
1.
0
0
0
0
0
0
0
0
0
0
1.
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
二楼
746


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

您是否介意上载您的xml文件,以便我可以测试我的解决方案?更改
var_dump($attrib['url')
打印($attrib['url'])并将输出粘贴到问题中首先对$result进行json_解码,然后遍历公平您必须将$attrib['url']键入string
var_dump((string)$attrib['url'])谢谢Christoph..按我需要的方式工作。不知道为什么我从未想过..真的很感激我使用了if语句来避免这种情况
<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>
<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"/>