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
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','barand@xxx.com');
$entry->appendChild($child);

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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