schenkkp Posted February 1, 2011 Share Posted February 1, 2011 I am trying to write a PHP page that takes an XML file stored on an external server, parses it, and rewrites the data to a new XML file formatted to my liking. The feed is for events and has about 250 events to parse. The problem is that if one event has an empty node, the script will use the same event data for the node used previously. Upon research, I see that empty nodes are parsed as an empty SimpleXMLElement Object. See my code for example: XML Original: <event_info> <event id="1222209"> <outcome_score><![CDATA[L, 1-0 (OT)]]></outcome_score> <quotes></quotes> <radio>102.9 FM</radio> <sched_opp><![CDATA[Toledo]]></sched_opp> </event> <event id="1222210"> <outcome_score><![CDATA[]]></outcome_score> <quotes></quotes> <radio>107.1 FM</radio> <sched_opp><![CDATA[Ohio]]></sched_opp> </event> </event_info> Output XML (note <outcome_score> on second event): <event_info> <event id="1222209"> <outcome_score><![CDATA[L, 1-0 (OT)]]></outcome_score> <quotes></quotes> <radio>102.9 FM</radio> <sched_opp><![CDATA[Toledo]]></sched_opp> </event> <event id="1222210"> <outcome_score><![CDATA[L, 1-0 (OT)]]></outcome_score> <quotes></quotes> <radio>107.1 FM</radio> <sched_opp><![CDATA[Ohio]]></sched_opp> </event> </event_info> PHP code: $xml = simplexml_load_file($source); echo "<event_info>"; foreach($xml->event as $event) { echo "<event>"; if($event->outcome_score != '') { echo "<outcome_score><!CDATA[{$event->outcome_score]]></outcome_score>"; } if($event->quotes != '') { echo "<quotes><!CDATA[{$event->quotes]]></quotes>"; } } if($event->radio != '') { echo "<radio><!CDATA[{$event->radio]]></radio>"; } if($event->sched_opp != '') { echo "<sched_opp><!CDATA[{$event->sched_opp]]></sched_opp>"; } echo "</event>"; } echo "</event_info>"; Basically, whenever an event has no outcome score, the most recent outcome score is repeated for all events after. Any help would be greatly appreciated! Thanks! Quote Link to comment Share on other sites More sharing options...
requinix Posted February 1, 2011 Share Posted February 1, 2011 Did you rewrite the code you used just for the post? Because it won't produce the same output you've posted, and that's not counting for the syntax errors. Quote Link to comment Share on other sites More sharing options...
schenkkp Posted February 1, 2011 Author Share Posted February 1, 2011 Did you rewrite the code you used just for the post? Because it won't produce the same output you've posted, and that's not counting for the syntax errors. Yes. Sorry. The real code is extremely long and there are about 50-60 fields. Do you understand what I am talking about? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 2, 2011 Share Posted February 2, 2011 I understand the problem, yes, but I could only throw out guesses as to what might be wrong. If I could see actual code then I could probably give an actual answer. Look for any place you have a loop, and then inside the loop you do not reference the variable. You go from a node outside the loop to someplace inside. Like foreach ($xml->event as $node) { // ... $xml->event->outcome_score // instead of $node->outcome_score // ... } My point is that there are other causes. That one above is a common problem that might manifest itself as the bug you have now. Quote Link to comment Share on other sites More sharing options...
schenkkp Posted February 8, 2011 Author Share Posted February 8, 2011 Hmm... That didn't seem to work. Any other ideas? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.