PHP新手专用的场景,关于如何使用SimpleXML解析存储在变量中的XML值


PHP newbie- specific scenario on how to parse an XML value stored in a variable using SimpleXML

我想使用Simple XML解析存储在变量中的XML数据。

这就是我所说的数据:

<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);

现在,我想获得上面XML数据中显示的"message"值。

当我尝试

foreach($xml->message[0]->text[0] as $response)

它不工作。

当我尝试像下面的代码我得到一个错误在Netbeans IDE

foreach($xml->SearchResults:searchresults[0]->message[0]->text[0] as $response)

我得到的错误是"unexpected: "

我如何正确地获取上述XML数据的消息?

我如何解析所有的"结果"元素,一个接一个?

如果您使用代码:

$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;

做一个var_dump($xml);来理解用SimpleXML加载后转换成对象和数组的XML结构。