Jump to content

[SOLVED] Appending xml using simplexml


pauljr8

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/61890-solved-appending-xml-using-simplexml/
Share on other sites

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','[email protected]');
$entry->appendChild($child);

$sxml = simplexml_import_dom($xml);
echo $sxml->asXML(); 

?>

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.