0

我想使用 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 数据中显示的“消息”值。

当我尝试

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

这没用。

当我尝试类似下面的代码时,我在 Netbeans IDE 中遇到错误

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

我得到的错误是“意外:”

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

另外,我如何一一解析所有“结果”元素?

4

1 回答 1

0

如果您使用代码:

$xml = simplexml_load_string($string);

$string变量包含 XML 时,第一个元素<SearchResults:searchresults>成为主要的SimpleXMLElement 对象,$xml而子标签<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 结构。

于 2013-06-06T14:31:57.590 回答