PHP: grabbing SQL data and converting to XML

Joined
Jan 21, 2004
Messages
561
This is a tough one for me.

I'm trying to fetch SQL results as XML to be used in a PHP document. Right now, I have 2 files: page.php and xmlgrabber.php

The "page.php" has a template and stuff powering it, so I can't modify the headers in it. The "xmlgrabber" page connects to MySQL and displays the results in XML. Now, how do I get the data from "xmlgrabber.php" into "page.php"? I can't just include() because of the headers, and DOMobj->load() apparently won't load php files, just .xml.

Is there a trick out there you guys can tell me about?

Thanks for your help!

edit: just had a thought....
Maybe I'm going about this wrong! Should I maybe be loading the xmlgrabber page into page.php using ajax xmlhttprequest?

edit 2: just tried it
It loaded the data into page.php, but it lost the xml formatting. Am I on the correct path at least? Is this where xslt comes into play?
 
You can put ob_start() at the top of your main page, require the file, and put ob_end_flush() at the bottom of the page. This should buffer all the output and allow you to overwrite the headers as needed, and then empty the buffer to the screen at the end. Hope that helps.
 
That kinda worked, but ruined the style of my main page since it was text/html, converting it to xml using the xmlgrabber.php broke it.

Good to know for the future though, thanks.
 
Well, like mentioned output buffering is probably your ideal solution/answer. You can modify the headers with header() to convert back to an xml type.

Another idea is you could run xmlgrabber and output it's conversion of the sql result to an xml file and include it in the page.php file. This seems like a lot of over header for each page load though, so unless you are purposely caching the data, output buffering is the way to go.
 
Back
Top