Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Using PHP to Create Class Using SimpleXML

I am trying to create as simple class to read and write and XML file.  It works when I put it in a script by itself, but doesn't work when I put it into a class.  Here's the script (attached).  The error I get is:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : XML declaration allowed only at the start of the document in [file] on line 57

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version='1.0' standalone='yes'?> in [file] on line 57

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in [file] on line 57

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in [file]:57 Stack trace: #0 [file](57): SimpleXMLElement->__construct('??<?xml version...') #1 [file](95): crud->writeXML() #2 {main} thrown in [file] on line 57
class crud {

	public $e = NULL;

	function writeXML() {
		$xmlstr = <<<XML
		<?xml version='1.0' standalone='yes'?>
		  <title>PHP: Behind the Parser</title>
			<name>Ms. Coder</name>
			<actor>Onlivia Actora</actor>
			<name>Mr. Coder</name>
			<actor>El Act&#211;r</actor>
		   So, this language. It's like, a programming language. Or is it a
		   scripting language? All is revealed in this thrilling horror spoof
		   of a documentary.
		   <line>PHP solves all my web problems</line>
		  <rating type="thumbs">10</rating>
		  <rating type="stars">10</rating>

		$movies = new SimpleXMLElement($xmlstr);

		$xml = $movies->asXML();

		$filename = 'test.xml';
		$this->saveFile($filename, $xml);

	function saveFile($filename, $data)
		if (is_writable($filename)) {

			// In our example we're opening $filename in append mode.
			// The file pointer is at the bottom of the file hence
			// that's where $somecontent will go when we fwrite() it.
			if (!$handle = fopen($filename, 'w')) {
				 $this->e = "Cannot open file ($filename)";

			// Write $somecontent to our opened file.
			if (fwrite($handle, $data) === FALSE) {
				$this->e = "Cannot write to file ($filename)";


		} else {
			$this->e = "The file $filename is not writable";

$crud = new crud;

Open in new window

1 Solution
käµfm³d 👽Commented:
Remove the whitespace before the XML declaration:


		$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>


Open in new window

EMB01Author Commented:
Gracias amigo!  I can't believe little stuff like that effects it so greatly.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now