Using PHP to Create Class Using SimpleXML

Posted on 2011-10-21
Last Modified: 2012-05-12
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

Question by:EMB01
    LVL 74

    Accepted Solution

    Remove the whitespace before the XML declaration:

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

    Open in new window

    LVL 14

    Author Closing Comment

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now