0

I want to parse an XML data stored in a variable, using Simple XML.

THis is the data I am talking about:

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

Now the above type of XML is stored in variable named $zillow_data

First I load it using SimpleXML using the code

$xml = simplexml_load_string($zillow_data);

Now, I want to get the "message" value as shown in the XML data above.

When I try

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

It does not work.

When I try something like the below code I get an error in Netbeans IDE

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

The error I get is "unexpected : "

How do I correctly fetch the message in above XML data?

Also how do I parse through all the "result" elements, one by one?

10
  • Can you provide a more complete, valid XML example? Commented Jun 6, 2013 at 13:43
  • Give valid xml, you should use getNamespaces() function Commented Jun 6, 2013 at 13:49
  • @Arvind, your XML is not valid, when loading it with SimpleXMLElement it throws namespace error : Namespace prefix SearchResults on searchresults is not defined. Can you fix that? Commented Jun 6, 2013 at 13:59
  • @RolandoIsidoro The word "SearchResults" is only used at the start and end of XML response- I think this may be related to the fact that there is a ":" in <SearchResults:searchresults -- in any case kindly let me know how to parse "<SearchResults:searchresults " because when I try xml->ELEMENT_NAME with the ":" it gives an error? Thanks Commented Jun 6, 2013 at 14:22
  • Your XML has errors, you could say it is invalid. Therefore a parser does not precisely know what to do with it, hence it can disappear or change (albeit this is a grey-area as it was invalid in the first place, so you can't change wrong into right, so wrong is probably "changed" into wrong): eval.in/32925 - output it to see what exactly simplexml will read here. Then decide how to access an element. Commented Jun 6, 2013 at 14:49

1 Answer 1

0

If You use the code:

$xml = simplexml_load_string($string);

while the $string variable contains the XML, the first element <SearchResults:searchresults> becomes the main $xml SimpleXMLElement object, while the child tags <request>, <message> and <response> are its properties.

Thus, forgetting about the undefined namespace warnings, You should be able to do e.g.:

foreach($xml->response->results->result as $result) {
    echo (string) $result->zpid;
}

There is only one message with only one text element, thus if You want to echo this one, You should only do:

echo (string) $xml->message->text;

Do a var_dump($xml); to understand the XML structure being transformed into objects and arrays after loading it with SimpleXML.

Sign up to request clarification or add additional context in comments.

3 Comments

@shadyx your code is correct- there is just one (small) error--> please put "->result" after "->results in the foreach loop code-- then I will accept your answer as the right answer. Thanks, Arvind
@Arvind Added, probably forgot to put it there first time ;-) Thanks for pointing out.
I understood that it was a typo:) just pointed out so that others who see this and are struggling with simpleXML/php (like me:) ) understand it correctly:) Thanks a lot, Arvind.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.