note 60471 added to ref.dom
| From: | me at somewhere dot com | Date: | Sun, 08 Jan 2006 03:56:29 +0000 |
| Subject: | note 60471 added to ref.dom | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101383@lists.php.net to get a copy of this message | ||
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();
?>
----
Manual Page -- http://www.php.net/manual/en/ref.dom.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60471
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60471&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60471&report=yes
Search -- http://master.php.net/manage/user-notes.php