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 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料