title: PHP 8.1.2 - Unusual behavior with DOMDocument when saving and formatting XML data
body: <p>I am trying to output some configuration data to an XML file using DOMDocument and the code below using loadXML(), saveXML() and save(), however the XML file always has a structure like below (notice <strong>Configuration/</strong>, but not <strong>Configuration /Configuration</strong>), and I cannot get XML indentation to work using preserveWhiteSpace = false and formatOutput = true. Where is &quot;<strong>Configuration/</strong>&quot; coming from, and why no closing clause after Authentication_Key_2?</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;Configuration/&gt;
  &lt;FullProductName&gt;Text&lt;/FullProductName&gt;
  &lt;Copyright&gt;Text&lt;/Copyright&gt;
  &lt;Version&gt;Text&lt;/Version&gt;
  &lt;Website&gt;Text&lt;/Website&gt;
  &lt;Database_Password&gt;NDE2NDYxNmQ0ODZmNmU2NTZiMzEzOTM4MzE=&lt;/Database_Password&gt;
  &lt;Authentication_Key_1&gt;MDY0ZmRhMDYwYzkxMjVkNzk1M2U4YzUx&lt;/Authentication_Key_1&gt;
  &lt;Authentication_Key_2&gt;ZjllYWFjYmQ5NTEyYmNiNWE4MmYwZj==&lt;/Authentication_Key_2&gt;
  </code></pre>
  <p>The code I'm using follows below:</p>
  <pre><code>//Create the config data structure.
  $new_config_data = new DOMDocument(&quot;1.0&quot;, &quot;UTF-8&quot;); 
  
  
  $config_xml_element_name[0] = &quot;Configuration&quot;;
  $config_xml_element_name[1] = &quot;FullProductName&quot;;
  $config_xml_element_name[2] = &quot;Copyright&quot;;
  $config_xml_element_name[3] = &quot;Version&quot;;
  $config_xml_element_name[4] = &quot;Website&quot;;
  $config_xml_element_name[5] = &quot;Database_Password&quot;;
  $config_xml_element_name[6] = &quot;Authentication_Key_1&quot;;
  $config_xml_element_name[7] = &quot;Authentication_Key_2&quot;;                   
  
  
  $config_xml_element_instance[0] = $new_config_data-&gt;createElement($config_xml_element_name[0]); 
  $config_xml_element_instance[1] = $new_config_data-&gt;createElement($config_xml_element_name[1], $FullProductName);
  $config_xml_element_instance[2] = $new_config_data-&gt;createElement($config_xml_element_name[2], $ThisProductSoftwareHouse);  
  $config_xml_element_instance[3] = $new_config_data-&gt;createElement($config_xml_element_name[3], $ThisProductVersion);
  $config_xml_element_instance[4] = $new_config_data-&gt;createElement($config_xml_element_name[4], $ThisProductWebsite);
  $config_xml_element_instance[5] = $new_config_data-&gt;createElement($config_xml_element_name[5], $_POST['postgresql-password']);
  $config_xml_element_instance[6] = $new_config_data-&gt;createElement($config_xml_element_name[6], $_POST['secret-key1']);  
  $config_xml_element_instance[7] = $new_config_data-&gt;createElement($config_xml_element_name[7], $_POST['secret-key2']);
  
  //Go through array of config file elements and append them to data structure
  for ($config_xml_element_id = 0; $config_xml_element_id &lt; count($config_xml_element_instance); $config_xml_element_id++) {
                              
          //If this is the Root element then append it to config data structure as Root 
          if ($config_xml_element_id == 0) {
              
              $new_config_data-&gt;appendChild($config_xml_element_instance[0]);                         
              
          //Otherwise append it to config data structure as Child element of Root element
          } else {
              
              $config_xml_element_instance[0] = $new_config_data-&gt;appendChild($config_xml_element_instance[$config_xml_element_id]);                          
              
          }
          
  }
  
  $Config_create_file_path = $_SERVER['DOCUMENT_ROOT'] .$ParentProductRootPath .$ParentProductSubPath .$ThisProductTopPath .&quot;/Application/Config/&quot;;
  $Config_create_file = ucfirst($ThisProductFilePrependName) .&quot;_config.xml&quot;;  
  $Config_create_file_string = $Config_create_file_path .$Config_create_file;
  
      
  //If the Config directory does not exist then create it
  if (!file_exists($Config_create_file_path)) {
  mkdir($Config_create_file_path, 0777, true);
  }
              
  
  //Save compiled config data structure to XML file       
  $ConfigDataXml = $new_config_data-&gt;saveXML();
  $new_config_data-&gt;preserveWhiteSpace = false;
  $new_config_data-&gt;formatOutput = true;  
  $new_config_data-&gt;loadXML($ConfigDataXml);
  $new_config_data-&gt;saveXML();                
  $config_write = $new_config_data-&gt;save($Config_create_file_string); 
  </code></pre>