Forum Moderators: coopster
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;
?>
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 :)