PHP DOM XML formatoutput在第一行添加空格

时间:2015-05-22 11:08:09

标签: php xml domdocument

我一直在努力解决这个问题,我无法弄清楚为什么xml文件的第一行会出现空格。

我正在将XML创建为字符串,如下所示:

$xml  = '<?xml version="1.0" encoding="utf-8" standalone="no" ?>';
$xml .= '<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.03_01">';

以下图片代表上述代码。

enter image description here

然后我以这种方式保存文件

$xml = new DOMDocument('1.0');
$xml->preserveWhiteSpace = FALSE;
$xml->formatOutput = TRUE;
$xml->loadXML($xmlString);
$xml->save('myfile.xml');

现在的问题是该文件在第一行包含空格。 我该如何删除它?我尝试过没有任何成功ltrim($xmlString);

enter image description here

1 个答案:

答案 0 :(得分:4)

<强>解决

我发现问题不在类select id, max(case when type = 'CC 1' then value end) as cc1, max(case when type = 'CC 2' then value end) as cc2, max(case when type = 'CC 3' then value end) as cc3, max(case when type = 'CC 4' then value end) as cc4 from <table t> group by id; 上,而是在允许用户下载文件的功能上。

以前我有这个:

DOMDocument

这段代码在下载后在文件中添加了额外的空格。要解决此问题,我必须在header('Content-Description: File Transfer'); header('Content-Type: text/xml'); header('Content-Disposition: attachment; filename=' . basename($file)); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); 之前添加ob_clean()