title: PHP: Create new node inside parent
body: <p>I have a root XML node called  and I am trying to add a new child called  to this but I am getting errors.  Inside  there is also children.  Here is my code:</p>
  
  <pre><code>    $xml = new DomDocument();
      $xml-&gt;load(X_ASSETS);
      $xml-&gt;formatOutput = true;
      $new_id = $this-&gt;getNewAssetId();
      // Root
      $xpath = new DOMXPath($xml);
      $assets = $xpath-&gt;query('assets');
      $xml_assets = $assets-&gt;item(0);
      $xml_root = $xml-&gt;createElement('asset');
      // Asset Name
      $xml_name = $xml-&gt;createElement('name');
      $xml_name-&gt;nodeValue = $clean_name;
      $xml_root-&gt;appendChild($xml_name);
      // Asset URL
      $xml_url = $xml-&gt;createElement('url');
      $xml_url-&gt;nodeValue = '/'.$name;
      $xml_root-&gt;appendChild($xml_url);
      // Asset ID
      $xml_id = $xml-&gt;createElement('id');
      $xml_id-&gt;nodeValue = $new_id;
      $xml_root-&gt;appendChild($xml_id);
  
  
      // Create our document and save
      $xml_assets-&gt;appendChild($xml_root);
      $xml-&gt;save(X_ASSETS);
  </code></pre>
  
  <p>I get the following error when running this:</p>
  
  <p>Fatal error: Call to a member function appendChild() on a non-object in /home/websites/zed_x/core/includes/x.inc on line 88</p>
  
  <p>Does anyone know what I am doing wrong here?</p>