php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42092 Unable to open xml via SimpleXML
Submitted: 2007-07-25 03:34 UTC Modified: 2007-07-25 14:28 UTC
From: paul at christianpatriot dot us Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.3 OS: Linux
Private report: No CVE-ID: None
 [2007-07-25 03:34 UTC] paul at christianpatriot dot us
Description:
------------
Attempting to append child/attributes to an existing xml file using post data from an html form. Post data gets passed fine, but it seems the xml file is never opened or loaded.
Since I don't think the file is ever opened, I am unable to (attempt to) write the new data to the file.

Reproduce code:
---------------
processgb.php
<?php
$xml = simplexml_load_file('gbook.xml');
$sxe = new SimpleXMLElement($xml);
$entry=$sxe->addChild('entry');
$entry->addAttribute('date',date("Y-m-d"));
$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($sxe,"gbook.xml"); if ($result<1 or result==false): echo 'error writing: '.$result;
endif;
?>

XML structure:
<?xml version="1.0" encoding="utf-8" ?>
<guestbook>
<entry date="">
	<name></name>
	<email></email>
	<homepage></homepage>
	<country></country>
	<comment></comment>
</entry>
</guestbook>

Expected result:
----------------
The new data is added to the xml file.

Actual result:
--------------
PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /usr/local/apache2/htdocs/ssa/9112001/guestbook/processgb.php:9 Stack trace: #0 /usr/local/apache2/htdocs/ssa/9112001/guestbook/processgb.php(9): SimpleXMLElement->__construct('') #1 {main}   thrown in /usr/local/apache2/htdocs/ssa/9112001/guestbook/processgb.php on line 9

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-25 14:28 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Your code looks wrong: simplexml_load_file() already creates an instance of the object, you don't have to create an instance of SimpleXMLElement yourself.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 09:01:27 2024 UTC