Forum Moderators: coopster

Message Too Old, No Replies

PHP and SimpleXML Implementation

         

username

11:31 pm on May 8, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all, I am testing the beginning of a SimpleXML application running on a PHP4 primary install, which uses the .php5 extension to take advantage of PHP5's SimpleXML feature which is also installed on this server. I am able to render the XML elements as desired, but when using the addChild() functionality, the code does not save the addition to the XML file, nor will it add it to the top of the XML file so that it is the latest addition. Can someone please help. I have set the permissions on the file and folder to 777, and have tried $xml[0] etc to try and position the new <comment> node in the right place, but no luck so far...

The index.php5 file:

<?php
include 'example.php5';

$xml = new SimpleXMLElement($xmlstr);

/* Add new comment to xml file */
$newComment = $xml->addChild('comment');
$newComment->addChild('author', 'Mr. Parser');
$newComment->addChild('email', 'email');
$newComment->addChild('website', 'web');
$newComment->addChild('location', 'location');
$newComment->addChild('text', 'text');
$newComment->addChild('date', 'monday');
$newComment->addChild('time', 'timer');

/* For each <comment> node, we echo the separate elements. */
foreach ($xml->comment as $comment) {
echo $comment->author, '<br />';
echo $comment->email, '<br />';
echo $comment->website, '<br />';
echo $comment->location, '<br />';
echo $comment->text, '<br />';
echo $comment->date, '<br />';
echo $comment->time, '<br /><br />';
}

?>

AND the PHP/XML file:

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<comments>
<comment>
<author>James Dean</author>
<email>support@email.com</email>
<website>http://www.website.com</website>
<location>Vegas</location>
<text>Some text that was entered. Hell yeah.</text>
<date>2008-03-02</date>
<time>13:04:03</time>
</comment>
<comment>
<author>Joe Smith</author>
<email>support@site.com</email>
<website>http://www.site.com</website>
<location>California</location>
<text>Something else...</text>
<date>2008-03-04</date>
<time>13:04:10</time>
</comment>
</comments>
XML;
?>

username

12:57 am on May 10, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



So just checking in, does anyone have any ideas on this PHP SimpleXML issue?

eelixduppy

1:45 am on May 10, 2008 (gmt 0)



It adds the child correctly, but you have to then write that to the file. In order to do that, though, you should keep your XML in a separate file by itself with no PHP. So for example:

comments.xml


<?xml version='1.0' standalone='yes'?>
<comments>
<comment>
<author>James Dean</author>
<email>support@email.com</email>
<website>http://www.website.com</website>
<location>Vegas</location>
<text>Some text that was entered. Hell yeah.</text>
<date>2008-03-02</date>
<time>13:04:03</time>
</comment>
<comment>
<author>Joe Smith</author>
<email>support@site.com</email>
<website>http://www.site.com</website>
<location>California</location>
<text>Something else...</text>
<date>2008-03-04</date>
<time>13:04:10</time>
</comment>
</comments>

Then your PHP should be adjusted as follows:


$xmlstr = [url=http://www.php.net/file-get-contents]file_get_contents[/url]("comments.xml");
#
$xml = new SimpleXMLElement($xmlstr);
#
/* Add new comment to xml file */
$newComment = $xml->addChild('comment');
$newComment->addChild('author', 'Mr. Parser');
$newComment->addChild('email', 'email');
$newComment->addChild('website', 'web');
$newComment->addChild('location', 'location');
$newComment->addChild('text', 'text');
$newComment->addChild('date', 'monday');
$newComment->addChild('time', 'timer');
#
$handle = [url=http://www.php.net/fopen]fopen[/url]("comments.xml", "w");
[url=http://www.php.net/fwrite]fwrite[/url]($handle, $xml->asXML());
[url=http://www.php.net/fclose]fclose[/url]($handle);

Good luck :)