title: PHP Copy xml From one XML file to another
body: <p>I'm very new to coding and have been thrown in at the depend but learning slowly! I have managed to create a script in PHP that logs into a property feed website in order to obtain a list of current properties listed by a seller.</p>
  <p>This is a 3 step process to get to the final XML property file. (Step:1 authenticates, Step:2 creates an XML file of all properties by ID and Step:3 provides an XML file containing all property specific details based on a property ID obtained in step 2.</p>
  <p>Here is the current script for Step:3:</p>
  <pre><code>        public function getProperties($client_id) {
          $token_storage = fopen('token.txt', 'r') or die(&quot;Unable to open file!&quot;);
          $token = trim(fread($token_storage,filesize(&quot;token.txt&quot;)));
          fclose($token_storage);
          $url = &quot;http://webservices.vebra.com/export/&quot;.$this-&gt;_datafeedid.&quot;/v&quot;.$this-&gt;_version.&quot;/branch/{$client_id}/property&quot;;
          
          $headers = array (
              &quot;Content-type: application/xml&quot;,
              &quot;Connection: close&quot;,
              &quot;Authorization: Basic &quot;.base64_encode($token)
          );
      
          $ch = curl_init(); 
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          curl_setopt($ch, CURLOPT_TIMEOUT, 10);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  
          
          
          $all_properties_id_only = curl_exec($ch);
          
  
          file_put_contents ('./all_properties_id_only.xml', $all_properties_id_only); 
          
          //return $data;
  
              
   $xml=simplexml_load_file(&quot;./all_properties_id_only.xml&quot;) or die(&quot;Error: Cannot create object&quot;);
   $count = 0;
   foreach($xml-&gt;children() as $property) {
   $count = $count + 1;
   echo  &quot;count = &quot; .$count.&quot; property = &quot;.$property-&gt;prop_id.&quot;&lt;br&gt;&quot; ;
  
    
    $url = &quot;http://webservices.vebra.com/export/&quot;.$this-&gt;_datafeedid.&quot;/v&quot;.$this-&gt;_version.&quot;/branch/{$client_id}/property/{$property-&gt;prop_id}&quot;;
          echo $property-&gt;prop_id . '&lt;br&gt;';
  
          $headers = array (
              &quot;Content-type: application/xml&quot;,
              &quot;Connection: close&quot;,
              &quot;Authorization: Basic &quot;.base64_encode($token)
          );
          
          $ch = curl_init(); 
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          curl_setopt($ch, CURLOPT_TIMEOUT, 10);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          
      $all_properties_full = curl_exec($ch);
          
      file_put_contents ('./all_properties_full.xml', $all_properties_full, FILE_APPEND); 
          //return $data;
  
  
  }
  }
  </code></pre>
  <p>The problem I have is that the final Step:3 generated XML file returned from the property server contains the XML data <code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</code> so when I append each property to the 'all_properties_full.xml' XML file it becomes invalid XML. (It also does not have a required tag <code>&lt;properties&gt;&lt;/properties&gt;</code></p>
  <p>I figured that I could use a template XML file (properties_template.xml) with the initial tags required and then iterate over 'all_properties_full.xml' to produce the final XML file (merged.xml) with the correct structure and details of each property.</p>
  <p>I found something similar here on StackOverflow but could not get it to work due to the following errors:</p>
  <p>PHP Warning:  DOMDocument::load(): XML declaration allowed only at the start of the document in /home/
  PHP Warning:  DOMDocument::load(): Extra content at the end of the document in /home/
  PHP Fatal error:  Uncaught Error: Call to undefined method DOMNodeList::property() in /home/</p>
  <pre><code>        $doc1 = new DOMDocument();
          $doc1-&gt;load('./properties_template.xml');
  
          $doc2 = new DOMDocument();
          $doc2-&gt;load('./all_properties_full.xml');
          
  
  $target = $doc1-&gt;getElementsByTagName('properties')-&gt;property(0);
  
  // iterate over 'property' elements of document 2
  $properties2 = $doc2-&gt;getElementsByTagName('property');
  for ($i = 0; $i &lt; $properties2-&gt;length; $i ++) {
      $properties2 = $properties2-&gt;property($i);
  
      // import/copy item from document 2 to document 1
      $property1 = $doc1-&gt;importNode($properties2, true);
  
      // append imported item to document 1 'res' element
      $target-&gt;appendChild($property1);
  
  }
  $doc1-&gt;save('merged.xml'); //edited -added saving into xml file 
  </code></pre>
  <p>Step:3 XML data that is returned from the property server is in this format:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;property xsi:noNamespaceSchemaLocation=&quot;http://webservices.vebra.com/export/xsd/v11/exportapi.xsd&quot; id=&quot;00000001&quot; propertyid=&quot;12345678&quot; system=&quot;A&quot; firmid=&quot;0001&quot; branchid=&quot;1&quot; database=&quot;1&quot; featured=&quot;0&quot;&gt;
  &lt;reference&gt;
  &lt;/reference&gt;
  &lt;address&gt;
  &lt;/address&gt;
  &lt;price qualifier=&quot;Guide Price&quot; currency=&quot;GBP&quot; display=&quot;yes&quot;&gt;250000&lt;/price&gt;
  &lt;rentalfees/&gt;
  &lt;lettingsfee/&gt;
  &lt;rm_qualifier&gt;0&lt;/rm_qualifier&gt;
  &lt;/property&gt;
  </code></pre>
  <p>This is the properties_template.xml file:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;properties&gt;
  &lt;property&gt;ADDITIONAL PROPERTY DETAILS HERE&lt;/property&gt;
  &lt;/properties&gt;
  </code></pre>
  <p>What I am ultimately trying to achieve with 'all_properties_full.xml' is the following structure:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;properties&gt;
  &lt;property xsi:noNamespaceSchemaLocation=&quot;http://webservices.vebra.com/export/xsd/v11/exportapi.xsd&quot; id=&quot;1&quot; propertyid=&quot;00000001&quot; system=&quot;A&quot; firmid=&quot;0001&quot; branchid=&quot;1&quot; database=&quot;1&quot; featured=&quot;0&quot;&gt;
  &lt;reference&gt;
  &lt;/reference&gt;
  &lt;address&gt;
  &lt;/address&gt;
  &lt;price qualifier=&quot;Guide Price&quot; currency=&quot;GBP&quot; display=&quot;yes&quot;&gt;250000&lt;/price&gt;
  &lt;rentalfees/&gt;
  &lt;lettingsfee/&gt;
  &lt;rm_qualifier&gt;0&lt;/rm_qualifier&gt;
  &lt;/property&gt;
  
  &lt;property xsi:noNamespaceSchemaLocation=&quot;http://webservices.vebra.com/export/xsd/v11/exportapi.xsd&quot; id=&quot;2&quot; propertyid=&quot;00000002&quot; system=&quot;A&quot; firmid=&quot;0001&quot; branchid=&quot;1&quot; database=&quot;1&quot; featured=&quot;0&quot;&gt;
  &lt;reference&gt;
  &lt;/reference&gt;
  &lt;address&gt;
  &lt;/address&gt;
  &lt;price qualifier=&quot;Guide Price&quot; currency=&quot;GBP&quot; display=&quot;yes&quot;&gt;250000&lt;/price&gt;
  &lt;rentalfees/&gt;
  &lt;lettingsfee/&gt;
  &lt;rm_qualifier&gt;0&lt;/rm_qualifier&gt;
  &lt;/property&gt;
  &lt;/properties&gt;
  </code></pre>
  <p>I'm also completely open to achieving this another way if there is a simpler/more efficient way of achieving the final XML file.</p>
  <p>Thank you for reading this far!</p>
  <p>SONewb</p>