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().

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

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA