I tried using DOMDocument and the code below to output some configuration data to an XML file using loadXML(), saveXML() and save(), but the XML file always has the following structure (note Configuration /, But not Configuration/Configuration), and I can't get XML indentation to work using preserveWhiteSpace = false and formatOutput = true. Where does "Configuration/" come from, and why is there no closing clause after Authentication_Key_2?
<?xml version="1.0" encoding="UTF-8"?> <Configuration/> <FullProductName>Text</FullProductName> <Copyright>Text</Copyright> <Version>Text</Version> <Website>Text</Website> <Database_Password>NDE2NDYxNmQ0ODZmNmU2NTZiMzEzOTM4MzE=</Database_Password> <Authentication_Key_1>MDY0ZmRhMDYwYzkxMjVkNzk1M2U4YzUx</Authentication_Key_1> <Authentication_Key_2>ZjllYWFjYmQ5NTEyYmNiNWE4MmYwZj==</Authentication_Key_2>
The code I used is as follows:
//Create the config data structure. $new_config_data = new DOMDocument("1.0", "UTF-8"); $config_xml_element_name[0] = "Configuration"; $config_xml_element_name[1] = "FullProductName"; $config_xml_element_name[2] = "Copyright"; $config_xml_element_name[3] = "Version"; $config_xml_element_name[4] = "Website"; $config_xml_element_name[5] = "Database_Password"; $config_xml_element_name[6] = "Authentication_Key_1"; $config_xml_element_name[7] = "Authentication_Key_2"; $config_xml_element_instance[0] = $new_config_data->createElement($config_xml_element_name[0]); $config_xml_element_instance[1] = $new_config_data->createElement($config_xml_element_name[1], $FullProductName); $config_xml_element_instance[2] = $new_config_data->createElement($config_xml_element_name[2], $ThisProductSoftwareHouse); $config_xml_element_instance[3] = $new_config_data->createElement($config_xml_element_name[3], $ThisProductVersion); $config_xml_element_instance[4] = $new_config_data->createElement($config_xml_element_name[4], $ThisProductWebsite); $config_xml_element_instance[5] = $new_config_data->createElement($config_xml_element_name[5], $_POST['postgresql-password']); $config_xml_element_instance[6] = $new_config_data->createElement($config_xml_element_name[6], $_POST['secret-key1']); $config_xml_element_instance[7] = $new_config_data->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 < 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->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->appendChild($config_xml_element_instance[$config_xml_element_id]); } } $Config_create_file_path = $_SERVER['DOCUMENT_ROOT'] .$ParentProductRootPath .$ParentProductSubPath .$ThisProductTopPath ."/Application/Config/"; $Config_create_file = ucfirst($ThisProductFilePrependName) ."_config.xml"; $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->saveXML(); $new_config_data->preserveWhiteSpace = false; $new_config_data->formatOutput = true; $new_config_data->loadXML($ConfigDataXml); $new_config_data->saveXML(); $config_write = $new_config_data->save($Config_create_file_string);
P粉4914214132024-03-30 10:23:18
You add all nodes to the root of the document, so there is no hierarchy, and the configuration node has no content (it shows "" because it is empty.
I think the line where the problem is is from the position after element id is added to 0...
$config_xml_element_instance[0] = $new_config_data->appendChild($config_xml_element_instance[$config_xml_element_id]);
Adding new elements to the root of the new document. To add it to a configuration element, add it to $config_xml_element_instance[0]
...
$config_xml_element_instance[0]->appendChild($config_xml_element_instance[$config_xml_element_id]);