我想使用简单的XML解析存储在变量中的XML数据。
THis就是我所说的数据:
<SearchResults:searchresults xsi:schemaLocation="http://www.zillow.com/static/xsd/SearchResults.xsd /vstatic/ae1bf8a790b67ef2e902d2bc04046f02/static/xsd/SearchResults.xsd">
<request>
<address>2114 Bigelow Ave</address>
<citystatezip>Seattle, WA</citystatezip>
</request>
<message>
<text>Request successfully processed</text>
<code>0</code>
</message>
<response>
<results>
<result>
<zpid>48749425</zpid>
<links>
<homedetails>http://www.zillow.com/homedetails/2114-Bigelow-Ave-N-Seattle-WA-98109/48749425_zpid/</homedetails>
<graphsanddata>http://www.zillow.com/homedetails/charts/48749425_zpid,1year_chartDuration/?cbt=7522682882544325802%7E9%7EY2EzX18jtvYTCel5PgJtPY1pmDDLxGDZXzsfRy49lJvCnZ4bh7Fi9w**</graphsanddata>
<mapthishome>http://www.zillow.com/homes/map/48749425_zpid/</mapthishome>
<comparables>http://www.zillow.com/homes/comps/48749425_zpid/</comparables>
</links>
<address>
<street>2114 Bigelow Ave N</street>
<zipcode>98109</zipcode>
<city>Seattle</city>
<state>WA</state>
<latitude>47.63793</latitude>
<longitude>-122.347936</longitude>
</address>
<zestimate>
<amount currency="USD">1219500</amount>
<last-updated>11/03/2009</last-updated>
<oneWeekChange deprecated="true"/>
<valueChange duration="30" currency="USD">-41500</valueChange>
<valuationRange>
<low currency="USD">1024380</low>
<high currency="USD">1378035</high>
</valuationRange>
<percentile>0</percentile>
</zestimate>
<localRealEstate>
<region id="271856" type="neighborhood" name="East Queen Anne">
<zindexValue>525,397</zindexValue>
<zindexOneYearChange>-0.144</zindexOneYearChange>
<links>
<overview>http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/</overview>
<forSaleByOwner>http://www.zillow.com/homes/fsbo/East-Queen-Anne-Seattle-WA/</forSaleByOwner>
<forSale>http://www.zillow.com/east-queen-anne-seattle-wa/</forSale>
</links>
</region>
<region id="16037" type="city" name="Seattle">
<zindexValue>381,764</zindexValue>
<zindexOneYearChange>-0.074</zindexOneYearChange>
<links>
<overview>http://www.zillow.com/local-info/WA-Seattle/r_16037/</overview>
<forSaleByOwner>http://www.zillow.com/homes/fsbo/Seattle-WA/</forSaleByOwner>
<forSale>http://www.zillow.com/seattle-wa/</forSale>
</links>
</region>
<region id="59" type="state" name="Washington">
<zindexValue>263,278</zindexValue>
<zindexOneYearChange>-0.066</zindexOneYearChange>
<links>
<overview>http://www.zillow.com/local-info/WA-home-value/r_59/</overview>
<forSaleByOwner>http://www.zillow.com/homes/fsbo/WA/</forSaleByOwner>
<forSale>http://www.zillow.com/wa/</forSale>
</links>
</region>
</localRealEstate>
</result>
</results>
</response>
</SearchResults:searchresults>
现在,上述类型的XML存储在名为$zillow_data
的变量中
首先,我使用下面的代码使用SimpleXML加载它
$xml = simplexml_load_string($zillow_data);
现在,我希望获得"message“值,如上面的XML数据所示。
当我尝试的时候
foreach($xml->message[0]->text[0] as $response)
它不起作用。
当我尝试执行类似以下代码的操作时,在Netbeans IDE中遇到错误
foreach($xml->SearchResults:searchresults[0]->message[0]->text[0] as $response)
我得到的错误是“意外的:”
如何正确获取上述XML数据中的消息?
另外,我如何逐个解析所有的"result“元素?
发布于 2013-06-06 14:31:58
如果您使用以下代码:
$xml = simplexml_load_string($string);
当$string
变量包含XML时,第一个元素<SearchResults:searchresults>
成为主$xml
SimpleXMLElement对象,而子标签<request>
、<message>
和<response>
是它的属性。
因此,忘记undefined namespace
警告,您应该能够这样做:
foreach($xml->response->results->result as $result) {
echo (string) $result->zpid;
}
只有一个message
只有一个text
元素,因此如果您想回显这个元素,您应该只做:
echo (string) $xml->message->text;
使用SimpleXML加载后,执行var_dump($xml);
以了解转换为对象和数组的XML结构。
https://stackoverflow.com/questions/16963659
复制相似问题