javascript – Problem with parsing xml file using php. Url address doesn’t contain .xml extension – Education Career Blog

Hey guys. I’m trying to parse xml file in order to extract some data from it and display it on the website. However, when I’m using url which doesn’t end with .xml extension I can’t run a foreach loop on the result. In other words when I’m using this kind of url:

$url = http://www.example.com/some_xml_file.xml

I can run a a foreach loop on it and it works like charm. But this time around I have to deal with this type of url:

$url = http://www.example.com/?some_var=something&some=something

So you can clearly see that it doesn’t contain .xml extension. It returns nicely parsed result but I can’t get the foreach loop to work on this result and therefore I can’t extract any data from it. Could you help me with this one?

Example of what I’m doing:

$url = 'http://www.example.com/?some_var=something&some=somethinge';

    $sx = simplexml_load_file($url);

    foreach ($sx->response as $row)
    {
        echo $row'status';

    }

And this is the result of var_dump($sx):

object(SimpleXMLElement)#334 (2) { "@attributes"=>  array(2) { "status"=>  string(2) "ok" "version"=>  string(3) "1.0" } "events"=>  object(SimpleXMLElement)#333 (2) { "@attributes"=>  array(3) { "resultCount"=>  string(1) "1" "pageSize"=>  string(2) "15" "pageIndex"=>  string(1) "0" } "event"=>  object(SimpleXMLElement)#338 (10) { "@attributes"=>  array(1) { "id"=>  string(6) "180085" } "name"=>  string(27) "Lady Gaga Monster Ball Tour" "doordatetime"=>  string(19) "2010-11-01T18:30:00" "tickets"=>  object(SimpleXMLElement)#337 (1) { "ticket"=>  array(5) { 0=>  object(SimpleXMLElement)#339 (7) { "@attributes"=>  array(1) { "id"=>  string(6) "307902" } "price"=>  string(7) "£55.00" "url"=>  string(32) "http://www.gigjunkie.net/T307902" "provider"=>  string(12) "Ticketmaster" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(5) "false" } 1=>  object(SimpleXMLElement)#340 (7) { "@attributes"=>  array(1) { "id"=>  string(6) "426248" } "price"=>  string(7) "£82.00" "url"=>  string(32) "http://www.gigjunkie.net/T426248" "provider"=>  string(8) "Seatwave" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } 2=>  object(SimpleXMLElement)#437 (6) { "@attributes"=>  array(1) { "id"=>  string(6) "306230" } "url"=>  string(32) "http://www.gigjunkie.net/T306230" "provider"=>  string(7) "Viagogo" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } 3=>  object(SimpleXMLElement)#438 (6) { "@attributes"=>  array(1) { "id"=>  string(6) "325819" } "url"=>  string(32) "http://www.gigjunkie.net/T325819" "provider"=>  string(15) "WorldTicketShop" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } 4=>  object(SimpleXMLElement)#342 (6) { "@attributes"=>  array(1) { "id"=>  string(6) "402593" } "url"=>  string(32) "http://www.gigjunkie.net/T402593" "provider"=>  string(14) "Empire Tickets" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } } } "venue"=>  object(SimpleXMLElement)#336 (9) { "@attributes"=>  array(1) { "id"=>  string(4) "1479" } "name"=>  string(13) "Odyssey Arena" "street"=>  string(13) "2 Queens Quay" "town"=>  string(7) "Belfast" "nameandtown"=>  string(22) "Odyssey Arena, Belfast" "country"=>  string(2) "GB" "gjurl"=>  string(90) "http://www.gigjunkie.net/gigs/Lady-Gaga-Monster-Ball-Tour/Odyssey-Arena/01-Nov-2010/180085" "latitude"=>  string(9) "54.602158" "longitude"=>  string(9) "-5.918215" } "gjurl"=>  string(90) "http://www.gigjunkie.net/gigs/Lady-Gaga-Monster-Ball-Tour/Odyssey-Arena/01-Nov-2010/180085" "artists"=>  object(SimpleXMLElement)#341 (1) { "artist"=>  object(SimpleXMLElement)#332 (5) { "@attributes"=>  array(2) { "id"=>  string(5) "54194" "isprimary"=>  string(4) "true" } "name"=>  string(9) "Lady Gaga" "gjurl"=>  string(42) "http://www.gigjunkie.net/artists/Lady-Gaga" "thumbnailimage"=>  string(68) "http://images.gigjunkie.net/f5376fab-cd01-4432-9205-8a1d0be41437.jpg" "mediumimage"=>  string(68) "http://images.gigjunkie.net/efbe624c-b63d-452a-a89b-221e846147ab.jpg" } } "iscancelled"=>  string(5) "false" "genre"=>  string(8) "Rock/Pop" "image"=>  string(68) "http://images.gigjunkie.net/efbe624c-b63d-452a-a89b-221e846147ab.jpg" } } } 

,

Just use the simplexml_load_file function, it will return False if can’t parse the xml

<?php
$url = 'http://www.example.com/?some_var=something&some=something'
if(simplexml_load_file($url)){
    // is a xml
else {
    // no xml found
}
?>

You can find more info here: http://www.php.net/manual/en/function.simplexml-load-file.php

,

Your processing doesn’t seem to match with your XML document structure. Your processing seems to be expecting a document like this:

<some_document>
  <response status="ok">...</response>
  <response status="ok">...</response>
  ...
</some_document>

However, by the looks of your var_dump, that’s not how the actual document is structured. That’s why you can’t iterate over the “response” elements with foreach() — I don’t think there are any there to iterate over.

If you can post the actual XML document, we can probably fix up your processing to match it.

The main thing to bear in mind, I think, is that the way SimpleXMLElement works is that the object you get back is the root of the XML document, so if your document looks like this:

<response>
   <some_data>...</some_data>
   <some_other_data>...</some_other_data>
</response>

…then $sx->response won’t exist. $sx is the <response> element, it doesn’t contain it.

Leave a Comment