0

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" } } } 
7
  • Can you post (at least the beginning of) your data retrieval and processing code, please? Commented Oct 15, 2010 at 9:45
  • Your new example is a bit wrong -- if you're doing simplexml_load_file(url) as Cristian suggests, you don't need the simplexml_load_string() as well. simplexml_load_file() will return an XML object directly, so you don't need the file_get_contents() or the simplexml_load_string(). Just use $sx = simplexml_load_file($url) and run the foreach across $sx as you are now. Commented Oct 15, 2010 at 9:56
  • are you suggesting doing something like:$sx = simplexml_load_file($url); foreach ($sx->response as $row) { echo $row['status']; } Commented Oct 15, 2010 at 10:00
  • @Pavel Yes, that should work. simplexml_load_file() creates a SimpleXML object, and you should be able to use that directly. It's basically a shortcut for what you were originally doing by loading the file into a string and then creating a SimpleXML object from the string. Commented Oct 15, 2010 at 10:06
  • when Im running var_dump($sx) it returns the result nicely - that's why I don't understand why I can't run a foreach loop on it :/ Commented Oct 15, 2010 at 10:09

2 Answers 2

2

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

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

6 Comments

That's not the case here. I know it's parsing it but I can't run the foreach loop on the parsed result.
Please post the result of: foreach ($sx->response as $row) { print_r($row); }
Also, if you're not getting any results from the foreach, it might be worth a sanity check by doing a var_dump($sx).
when Im running var_dump($sx) it returns the result nicely - that's why I don't understand why I can't run a foreach loop on it :/
@Pavel Can you add the results of the var_dump to your question, please? Might give us something to go on. Ta.
|
1

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.

3 Comments

So basically I should save the output in a file, say file.xml, put it on my server and then parse this one in order to extract the values I need, correct?
@Pavel No, what I'm saying is that you can do exactly what you're doing, but that your processing is slightly wrong compared with the structure of your XML file. Can you please post the actual XML source document you're trying to process?
Thanks Matt, I was finally able to solve that. Basically what I did was starting the loop from the second element in the tree. Nice hint!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.