note 60471 deleted from ref.dom by didou

From: Date: Sat, 30 Dec 2006 02:59:57 +0000
Subject: note 60471 deleted from ref.dom by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121032@lists.php.net to get a copy of this message
Note Submitter: me at somewhere dot com ---- When using include/require statements to bring in common PHP code, you may accidentally put a blank line after the trailing "?>" in the included file (ie. two newlines following the "?>" sequence). The output will contain these blank lines and, for outputting HTML, this is no biggie as web browsers are usually relaxed about parsing HTML syntax. However, XML output generated via DOMDocument->saveXML() will be checked more carefully, and XML parsers (like the one that comes with firefox/mozilla) will complain that the <?xml ....?> tag doesn't begin on the first line. While the best solution is to make sure blank lines do not trail in the included files, you can't always guarentee that a set of include files you grab from the internet will comply. Since the nature of DOM XML generation involves building the DOM structure first, then converting it all to XML, and finally printing it all in one shot, you can safely wipe any blank lines that snuck into the output buffer when files were getting included by calling ob_clear() right before printing/echoing the XML: dom.php: <?php $doc = new DOMDocument('1.0', 'iso-8859-1'); $doc->formatOutput = true; ?> ...garbage trailing at the end.... foobar.php: <?php include 'dom.php' //Build up $doc here header("Content-type: text/xml"); ob_clean(); echo $doc->saveXML(); ?>

« previous php.notes (#121032) next »