du8442 2015-05-22 11:08
浏览 221
已采纳

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

I've been struggling with this problem and I can't figure out why is there a white space in the first line of the xml file.

I'm creating the XML as string, like so:

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

The following image represents the code above.

enter image description here

And then I save the file this way

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

The problem now is that the file contains a space in the first line. How can I remove it? I've tried without any success ltrim($xmlString);

enter image description here

  • 写回答

1条回答 默认 最新

  • douyong8801 2015-05-22 11:31
    关注

    Solved.

    I found out that the problem wasn't on the class DOMDocument but yes on the function that allows the user to download the file.

    Previously I had this:

    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);
    

    And this piece of code was adding the extra whitespace in the file after download. To solve this I had to add ob_clean() before readfile().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。