PDA

View Full Version : Cant use php flush() on text/xml content?


misterk
09-10-2006, 02:13 AM
I have a php script that is querying a mysql DB and throwing back the results via <table>,<tr>,<td>'s . The result set may be very large so after each </tr> or </table> I issue a flush() or ob_flush() and everything works perfectly to keep impatient users seeing additional data rather then a blank screen.

However once i converted to returning the results in xml 1.0 it seems to break the ability to flush() and the page will not display until the entire results are returned. Specifically the:

header("Content-Type: text/xml;charset=ISO-8859-1");

seems to break all flush()'s. Is there any way to output buffer the xml formatted results without having to go an ajax route?

Thank in advance for anyone who may be able to help or offer advice.

Alex Vincent
09-10-2006, 05:02 AM
Hmm. The way I read your comment, you're asking the browser to render whatever it's received via the flush() call, and continue to wait for more markup from the server. XML doesn't work that way.

Unlike HTML, XML requires the whole document be well-formed before the XML reader (in this case, the browser) does anything with the document. Well-formedness cannot be guaranteed until the entire document has been received - the XML parser must receive the closing tag for the document element.

What exactly are you trying to do with the XML? Maybe there's another way.

misterk
09-10-2006, 05:18 AM
Alex,

Thank you for your thoughtful reply. What I am trying to do is use one script for "dual duty" so to speak. To serve as an API and also as a front end user tool. The reason I went to a tagged structure was to offer a cleaner, tighter output format which xml fits perfectly.

Example results:
<ROOT>
<ELEMENT>
<ID>100</ID>
<URL>http://www.yahoo.com</URL>
<VALUE>$50.00</value>
</ELEMENT>
<ELEMENT>
<ID>100</ID>
<URL>http://www.google.com</URL>
<VALUE>$75.00</value>
</ELEMENT>
<ELEMENT>
<ID>100</ID>
<URL>http://www.msn.com</URL>
<VALUE>$25.00</value>
</ELEMENT>
</ROOT>


XML works perfectly for nice tight results and also allows my users to directly query my server w/o even using a browser. Rather then the old method which was:

<table><tr><td></td><td></td><td></td></tr>........</table> which was less ideal for users to integrate with their own applications.

Does this make sense or am I justr rambling now? LOL

EDIT: Forgot to mention I use xsl to control the presentation so that non-api users see a "prettier" view then ppl hitting the api directly via script/bots/etc

liorean
09-12-2006, 01:57 AM
Hmm. The way I read your comment, you're asking the browser to render whatever it's received via the flush() call, and continue to wait for more markup from the server. XML doesn't work that way.

Unlike HTML, XML requires the whole document be well-formed before the XML reader (in this case, the browser) does anything with the document. Well-formedness cannot be guaranteed until the entire document has been received - the XML parser must receive the closing tag for the document element.Actually, I saw some Opera devs talk about this, and they reached the conclusion that as long as the document is well formed - to that point - incremental renedering is no less possible for XML than is it for HTML. It's just a feature that is missing from browsers that might very well start appearing in the future.