473,408 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

SimpleXML Question

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
2 1620
Dormilich
8,658 Expert Mod 8TB
$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
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

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

Similar topics

3
by: theboss3 | last post by:
Take, for example a typical RSS 2.0 feed (like http://lorelle.wordpress.com/feed/) and tell me how I access the tags like 'content:encoded' and 'wfw:commentRSS' using SimpleXML. I've searched far...
2
by: mandric | last post by:
Hello, Can someone please enlighten me on how to preserve the <!]> element when parsing an xml file or string with simplexml. I'm using libxml 2.6.16 and php 5.1.4. I tried a few variations,...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
3
by: bleen | last post by:
I'm trying to use SimpleXML but I've run into a conundrum. Every day an XML file is generated that this script grabs and manipulates. How can I check that the XML has no problems before creating...
5
by: Pablo | last post by:
Hello, Does anybody know any tutorial about using PHP5 SimpleXML with forms? I have found some SimpleXML examples, but only modifying node attributes from the php code directly. I would like...
0
by: ty | last post by:
I have a script that takes a xml template then adds data into it using SimpleXMLElement. I then save it using asXML into a mysql database. On my development machine at home it works fine. ...
5
by: SM | last post by:
Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <discography version="0.01"> <CD>...
7
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
1
by: SM | last post by:
Hello, I have a couple of XML files that represent articles. Each XML file is unique. Meaning that overall the structure is the same but some tags in the xml file are not in the same place or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.