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

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改