Solved

non-object and invalid argument

Posted on 2011-03-10
3
315 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 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