簡體   English   中英

將格式化的xml保存到PHP文件中

[英]Save formated xml to file in PHP

我知道這里有很多問題,但是我嘗試的每個答案都不能解決我的問題

問題

我已將xml保存在字符串上,我想將其保存到文件中,但又不想將xml放在一行中,我想以優美的打印效果寫入文件

也有是一個更大的問題,這一點,而不是這個"它顯示了這個"因為這個文件將在diferent網站,我不能加載"

注意, $content變量具有" not "

我知道\\n沒關系,但要使其更易於分析生成的xml文件,我需要將xml格式化

這就是我正在使用的

$dirname = "temp/" . uniqid();
mkdir($dirname);

$filename = $dirname . "/gen.xml";

$doc = new DomDocument($content);
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->save($filename);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=gen.xml");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

$fh = fopen($filename, 'a');
fclose($fh);
unlink($filename);
rmdir($dirname);

我也試過

header("Content-type: text/xml"); file_put_contents

DomDocument構造函數不接受xml字符串-而是可選的版本和編碼。

您可能想要的是改為加載$content xml字符串:

$doc = new DOMDocument();

$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadXML($content);

$doc->save($filename);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM