This question already has an answer here:
I am currently using php 5.5.15
This is the code I use to write a simple xml file called comment.xml using DOM. Now the structure of the file as illustrated below is what I require. What I would appreciate is code sample which will allows me to read all users and comments and out put them say to html. and also code sample to append to the file below.
any help much appreciated.
/*** a new dom object ***/
$dom = new domDocument;
/*** make the output tidy ***/
$dom->formatOutput = true;
/*** create the root element ***/
$root = $dom->appendChild($dom->createElement( "comments" ));
/*** create the simple xml element ***/
$sxe = simplexml_import_dom( $dom );
/*** add a user element ***/
$sxe->addChild("user", $User_Name);
/*** add a comment element ***/
$sxe->addChild("comment", $Comment);
$dom->save('comment.xml');
The output for the above code is:
<?xml version="1.0"?>
<comments>
<user>Joe Blogs</user>
<comment>This is a comment</comment>
</comments>
</div>