所以我的问题是,当我用一些 PHP 行编辑 XML 文件(如下)时,第一次一切正常,然后它会删除错误行并在错误的位置创建新行。
这里是用于删除行的 PHP 脚本:
<?php
session_start();
// The file
$filePath = '/....../test/test.xml';
// Grab file into an array, by lines
$fileArr = file($filePath);
// Remove desired line
unset($fileArr[7]); // $fileArr[15] == line #16
unset($fileArr[16]); // idem
unset($fileArr[25]); // idem
//Put back with PHP5
file_put_contents($filePath, implode('', $fileArr ));
session_destroy();
?>
这里是写新行的 PHP 脚本
<?php
session_start();
// The file
$filePath = '/....../test/test.xml';
// Grab file into an array, by lines
$fileArr = file($filePath);
//Change
$etichettaasse = "\t\t\t\t<string>\t14 gen</string>\n\r";
$medianord = "\t\t\t\t<number>\t280\t</number>\n\r";
$mediasud = "\t\t\t\t<number>\t280\t</number>\n\r";
// Replace line
$fileArr[11] = $etichettaasse;
$fileArr[19] = $medianord;
$fileArr[27] = $mediasud;
// Implode and save
file_put_contents($filePath, implode('', $fileArr ));
session_destroy();
?>
这里是原始XML:
<chart>
<axis_category color='ffffff' skip='0' size='12' alpha='80' />
<axis_value color='ffffff' skip='10' size='12' show_min='false' min="80" max="130" />
<chart_border top_thickness='1' bottom_thickness='1' left_thickness='1' right_thickness='1' />
<chart_data>
<row>
<null/>
<string> 3 dic</string>
<string> 10 dic</string>
<string> 17 dic</string>
<string> 24 dic</string>
<string> 31 dic</string>
</row>
<row>
<string>Media Nord</string>
<number> 102.72 </number>
<number> 101.60 </number>
<number> 101.85 </number>
<number> 101.84 </number>
<number> 101.84 </number>
</row>
<row>
<string>Media Sud</string>
<number> 102.28 </number>
<number> 101.24 </number>
<number> 101.70 </number>
<number> 101.88 </number>
<number> 101.88 </number>
</row>
</chart_data>
</chart>
第一轮PHP脚本后,按要求更新:
<chart>
<axis_category color='ffffff' skip='0' size='12' alpha='80' />
<axis_value color='ffffff' skip='10' size='12' show_min='false' min="80" max="130" />
<chart_border top_thickness='1' bottom_thickness='1' left_thickness='1' right_thickness='1' />
<chart_data>
<row>
<null/>
<string> 10 dic</string>
<string> 17 dic</string>
<string> 24 dic</string>
<string> 31 dic</string>
<string> 14 gen</string>
</row>
<row>
<string>Media Nord</string>
<number> 101.60 </number>
<number> 101.85 </number>
<number> 101.84 </number>
<number> 101.84 </number>
<number> 280 </number>
</row>
<row>
<string>Media Sud</string>
<number> 101.24 </number>
<number> 101.70 </number>
<number> 101.88 </number>
<number> 101.88 </number>
<number> 280 </number>
</row>
</chart_data>
PHP脚本第二轮之后:
<chart>
<axis_category color='ffffff' skip='0' size='12' alpha='80' />
<axis_value color='ffffff' skip='10' size='12' show_min='false' min="80" max="130" />
<chart_border top_thickness='1' bottom_thickness='1' left_thickness='1' right_thickness='1' />
<chart_data>
<row>
<null/>
<string> 17 dic</string>
<string> 24 dic</string>
<string> 14 gen</string>
<string> 14 gen</string>
</row>
<row>
<string>Media Nord</string>
<number> 101.60 </number>
<number> 101.84 </number>
<number> 280 </number>
<number> 280 </number>
<row>
<string>Media Sud</string>
<number> 101.24 </number>
<number> 101.70 </number>
<number> 280 </number>
</row>
<number> 280 </number>
如果我只能拥有一个 PHP 文件而不是 2 个,我也会很高兴,但我不知道该怎么做。
这是修改此 XML 文件的最佳方法,还是有其他方法?
请注意,用户将写入 HTML 的新行。
我有这个“开始”脚本用于删除我要删除的内容。
你怎么看呢?
<?php
$file = '/...../test.xml';
$fp = fopen(file,"w"); // open it for WRITING ("w")
if (flock($fp, LOCK_EX)) {
//do actions here
$xml = file_get_contents($file);
$sxe = simplexml_load_string($xml);
echo '<pre>';
// "delete the 1th string of row 1, etc."
unset($sxe->chart_data->row[0]->string[0]);
unset($sxe->chart_data->row[1]->number[0]);
unset($sxe->chart_data->row[2]->number[0]);
$sxe->chart_data->row[0]->addChild('string', 'test');
$sxe->chart_data->row[1]->addChild('number', '999');
$sxe->chart_data->row[2]->addChild('number', '9999');
file_put_contents($file, $sxe->asXML());
// unlock the file
flock($fp, LOCK_UN);
} else {
// flock() returned false, no lock obtained
print "Could not lock $file!\n";
}
?>