pauljr8 Posted July 26, 2007 Share Posted July 26, 2007 How do I add to the contents of gbook.xml? Tried everything I can think of :-( gbook.xml <guestbook> <entry date" "> <name></name> <email></email> <homepage></homepage> <country></country> <comment></comment> </entry></guestbook> <?php if (file_exists('gbook.xml')) { $xml = simplexml_load_file('gbook.xml'); } else { exit('Failed to open gbook.xml.'); } $entry=$xml->addChild('entry'); $entry->addAttribute('date',date("r")); $entry->addChild('name',$_POST["name"]); $entry->addChild('email',$_POST["email"]); $entry->addChild('homepage',$_POST["homepage"]); $entry->addChild('country',$_POST["country"]); $entry->addChild('comment',$_POST["comment"]); $result=file_put_contents("gbook.xml",$entry->asXML()); if ($result<1 or result==false): echo 'error writing: '.$result; endif; ?> When using: $entry=$xml->guestbook->addChild('entry'); gbook.xml is blanked. Using it as written, it replaces all other entries including the root element with the new entry so the next time an entry is attempted, it fails. Apache/2.2.3 (Unix) PHP/5.2.3 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 26, 2007 Share Posted July 26, 2007 You'd need a mix of DOM and simplexml <?php $xml = new domDocument; $gb = $xml->createElement('guestbook'); $xml->appendChild($gb); $entry=$xml->createElement('entry'); $gb->appendChild($entry); $entry->setAttribute('date', date('r')); $child = $xml->createElement('name','barand'); $entry->appendChild($child); $child = $xml->createElement('email','barand@xxx.com'); $entry->appendChild($child); $sxml = simplexml_import_dom($xml); echo $sxml->asXML(); ?> Quote Link to comment Share on other sites More sharing options...
pauljr8 Posted July 26, 2007 Author Share Posted July 26, 2007 Many thanks for your reply. The problem was 2 fold, I wanted to avoid using the dom if possible. Reading the php manual on simplexml caused me to think it was possible, hmmmm. Secondly, the gbook.xml already exists with data and the goal is to add new entry[date]/name/email/ etc. to the existing file, after receiving the data from a form post. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 26, 2007 Share Posted July 26, 2007 try <?php if (isset($_POST['sub'])) { $xml = new domDocument; $xml->load('gbook.xml'); $gblist = $xml->getElementsByTagname('guestbook'); $gb = $gblist->item(0); $entry=$xml->createElement('entry'); $gb->appendChild($entry); $entry->setAttribute('date', date('r')); $child = $xml->createElement('name',$_POST['name']); $entry->appendChild($child); $child = $xml->createElement('email',$_POST['email']); $entry->appendChild($child); $xml->save('gbook.xml'); } ?> <form method='post'> Name <input type="text" name="name"><br> Email <input type="text" name="email"><br> <input type="submit" name="sub" value="Add"> </form> Quote Link to comment Share on other sites More sharing options...
pauljr8 Posted July 27, 2007 Author Share Posted July 27, 2007 Thanks again for all your help. Of course there's not much (any) simplexml in the solution. Maybe it's just useful when creating web pages with php. I use XSLT to transform and display the xml data. Thanks again. 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.