By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

SimpleXML Question

P: 8
I'm attempting to use php and SimpleXML to assign variables from an XML output. This works:

Expand|Select|Wrap|Line Numbers
  1. $xml = simplexml_load_file($request_url) or die("feed not loading");
and when I do a:

Expand|Select|Wrap|Line Numbers
  1. echo "<pre>";
  2. var_dump($xml);
  3. echo "</pre>";
I get one of these two results depending on value of "Type" attribute.


Expand|Select|Wrap|Line Numbers
  1. object(SimpleXMLElement)#1 (2) {
  2.   ["@attributes"]=>
  3.   array(1) {
  4.     ["Type"]=>
  5.     string(5) "Popup"
  6.   }
  7.   ["ad"]=>
  8.   object(SimpleXMLElement)#2 (6) {
  9.     ["url"]=>
  10.     object(SimpleXMLElement)#3 (0) {
  11.     }
  12.     ["cpvrate"]=>
  13.     string(5) "0.005"
  14.     ["creativeid"]=>
  15.     string(10) "0204499385"
  16.     ["campaignid"]=>
  17.     string(12) "2-0210728303"
  18.     ["max-imp-per-user"]=>
  19.     string(1) "1"
  20.     ["frequency-period"]=>
  21.     string(2) "24"
  22.   }
  23. }
  24.  
  25.  
  26. object(SimpleXMLElement)#1 (1) {
  27.   ["@attributes"]=>
  28.   array(1) {
  29.     ["Type"]=>
  30.     string(7) "NoPopup"
  31.   }
  32. }

In my php code I'm assigning variables like this:

Expand|Select|Wrap|Line Numbers
  1. $adtype = (string) $xml->Type;
  2. $outurl = (string) $xml->ad->url;
  3. $cpvr = (string) $xml->ad->cpv_rate;
  4. $crid = (string) $xml->ad->creativeid;
  5. $caid = (string) $xml->ad->campaignid;
  6. $mipu = (string) $xml->ad->max-imp-per-user;
  7. $fp = (string) $xml->ad->frequency-period;
Only $outurl gets assigned. The other variables remain "empty". What should the correct syntax be for the other variable assignments?
May 3 '10 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
$xml->ad->max-imp-per-user & $xml->ad->frequency-period are invalid variable names (from the PHP side)

$xml->ad->cpv_rate to $xml->ad->frequency-period simply don’t exist, because $xml->ad->url is the object that contains these members (i.e. $xml->ad->url->cpv_rate).
May 4 '10 #2

P: 8
Thanks Dormilich for the feedback. I managed to get everything working and wanted to post my results for anyone else who happens upon this thread.

First off, url should have been at the same level as the other elements. Problem was that it was wrapped in a CDATA statement that screwed up the reading in of the file. I corrected that by using:

Expand|Select|Wrap|Line Numbers
  1. $xml = simplexml_load_file($request_url,'SimpleXMLElement', LIBXML_NOCDATA)
After doing that, the variable asignments that worked looked like this:
Expand|Select|Wrap|Line Numbers
  1. $adtype = (string) $xml['Type'];
  2. $outurl = (string) $xml->ad->url;
  3. $cpvr = (string) $xml->ad->cpvrate;
  4. $crid = (string) $xml->ad->creativeid;
  5. $caid = (string) $xml->ad->campaignid;
  6. $mipu = (string) $xml->ad->{'max-imp-per-user'};
  7. $fp = (string) $xml->ad->{'frequency-period'};
Thanks again for pointing me in the right direction.
May 5 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.