Jump to content

PHP Simple XML


schenkkp

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.