Solved

non-object and invalid argument

Posted on 2011-03-10
3
324 Views
Last Modified: 2013-12-13
Hi,

I have a problem with reading an xml file... and am not sure why I'm getting the error.
So here is a part of the xml:

<status>
<shard name="Blightweald" online="True" locked="False" population="high" queued="0" language="English" pvp="False" rp="False" recommend="False"/>
<shard name="Cloudborne" online="True" locked="False" population="high" queued="0" language="English" pvp="True" rp="False" recommend="False"/>
<shard name="Steampike" online="True" locked="False" population="high" queued="0" language="English" pvp="False" rp="False" recommend="False"/>
</status>

Open in new window



and here is my code:
$url = "http://eu.riftgame.com/en/status/eu-status.xml";

		// READ THE EXTERNAL DATA
		$xml = simplexml_load_file($url);

		foreach ($xml->status["shard"] as $shard)
		{
			if ($shard["name"] == "Blightweald")
			{
				echo $shard->name;
				echo $shard->population;
			}
		}

Open in new window


I even tried setting
$xml->status["shard"]

to

$xml->status->shard

But no use. That would be an invalid argument.

Any ideas?
0
Comment
Question by:Mutsop
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
zappafan2k2 earned 500 total points
ID: 35103085
When figuring this stuff out, print_r() is always helpful.  By running print_r($xml), I found that PHP was making "status" the root.

Try this:

$url = "http://eu.riftgame.com/en/status/eu-status.xml";

      // READ THE EXTERNAL DATA
      $xml = simplexml_load_file($url);

      foreach ($xml->shard as $shard)
      {
         if ($shard['name'] == "Blightweald")
         {
            echo $shard['name'];
            echo $shard['population'];
         }
      }

Open in new window

0
 
LVL 3

Author Comment

by:Mutsop
ID: 35103516
Oh okay... Well I thought the root had to be set too :)
Thanks, it works like a charm
0
 
LVL 3

Author Closing Comment

by:Mutsop
ID: 35103534
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Inserting data into database 10 46
PHP website on Linux - server DNS address could not be found. 18 77
restriction of entering a a page 5 29
Put POST values into cookies. 14 28
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question