title: PHP writing to XML File
body: <p>I have a problem with writing a specific string to a XML file.</p>
  
  <p>The XML structure is this:</p>
  
  <pre><code>&lt;ePrekrsaji&gt;
  &lt;time&gt;
  &lt;shift hours="48"/&gt;
  &lt;/time&gt;
  &lt;/ePrekrsaji&gt;
  </code></pre>
  
  <p>The code I use doesnt seem to work:</p>
  
  <pre><code>$my_file = "hours.xml";
  
  $hours = 5
  
  $xml = new DOMDocument();
  $xml-&gt;load($my_file);
  $xml_hours = $xml-&gt;createElement($hours);
  $nodes = $xml-&gt;getElementsByTagName('shift ') ;
  if ($nodes-&gt;length &gt; 0) {
     $xml-&gt;appendChild( $xml_hours );
  }
  $xml-&gt;save($my_file);
  </code></pre>
  
  <p>ERROR I GET
  Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in /var/www/WebDiP/2013_projekti/WebDiP2013_031/skripte/vrijeme.php:17 Stack trace: #0 /var/www/WebDiP/2013_projekti/WebDiP2013_031/skripte/vrijeme.php(17): DOMDocument->createElement('') #1 {main} thrown in /var/www/WebDiP/2013_projekti/WebDiP2013_031/skripte/vrijeme.php on line 17</p>
  
  <p>How to write to this specfic node?</p>
  
  <p>I want the end result to be:</p>
  
  <pre><code>&lt;ePrekrsaji&gt;
      &lt;time&gt;
      &lt;shift hours="5"/&gt;
      &lt;/time&gt;
  &lt;/ePrekrsaji&gt;
  </code></pre>