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