PHP 8.1.2 - 保存和格式化 XML 数据时 DOMDocument 的异常行为
P粉026665919
P粉026665919 2024-03-29 19:06:20
0
1
398

我尝试使用 DOMDocument 和下面的代码使用 loadXML()、saveXML() 和 save() 将一些配置数据输出到 XML 文件,但是 XML 文件始终具有如下结构(注意 Configuration /,但不是配置/配置),并且我无法使用preserveWhiteSpace = false 和formatOutput = true 使XML 缩进起作用。 “Configuration/”来自哪里,为什么 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>

我使用的代码如下:

//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粉026665919
P粉026665919

全部回复(1)
P粉491421413

您将所有节点添加到文档的根中,因此没有层次结构,并且配置节点没有内容(因为它是空的,所以显示“”。

我认为问题所在的行是从元素 id 添加到 0 之后的位置...

$config_xml_element_instance[0] = $new_config_data->appendChild($config_xml_element_instance[$config_xml_element_id]);

正在将新元素添加到新文档的根目录中。要将其添加到配置元素,请将其添加到 $config_xml_element_instance[0]...

$config_xml_element_instance[0]->appendChild($config_xml_element_instance[$config_xml_element_id]);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!