PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm using some AJAX logic to pull information out of the database and display that content on the basies of what a user chooses from a drop-down menu.
The PHP and Javascript works fine apart from the PHP generates invlaid XML and when i check the source code i can see it has genertaed a valid xml file except there is some white space before the xml declartion and i am not sure why? Can anyone help or give me some advice please?
thanks for the reply i know thats what is causing it but i am unsure why a whitespace is occuring i have use the trim() as well to no sucess any ideas?
Remove closing ?> tags on all files - they're not needed and this step automatically eliminates any whitespace at the end of a file (frequently after the closing tag) from being interpreted as output. I've stopped using the closing ?> and there is a noticeable lack of whitespace/output already sent type errors creeping into my code.
If you can't find the error quickly and it keeps dragging on, try using output buffering for that specific request (or other affected) so you can run trim() on the whole output and not just the part where you echo XML. That would basically be a band-aid - you still need to hunt down that bug to solve the root problem. Too many band aids just make your code unmaintainable over time.
Maugrim_The_Reaper wrote:If you can't find the error quickly and it keeps dragging on, try using output buffering for that specific request (or other affected) so you can run trim() on the whole output and not just the part where you echo XML.
I would suggest the opposite approach: force a "header already sent" warning because it contains the location of the output
<response />
i.e. the documentElement has no children. And that's why
//obtain the XML documents element
xmlRoot = xmlResponse.documentElement;
//testing that we received the XML document we expect
if(rootNodeName != "response" || !xmlRoot.firstChild)
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
This could just be me, but I'm against using PHP documents to generate XML within themselves when they need database interaction. It's way more querying than you need.
What you could try is to just use PHP to update them XML document once you've updated the content in the document, and only then. So, you'd use fopen, fwrite, and fclose. That's it. As long as you're familiar with how XML documents are created, you'd just make a string with all of these appropriate tags and loop through some database data, creating the items.
Maybe I haven't read carefully, enough, but this is the method I use. You give the file an '*.xml' extension and an XML header, and your server should automatically parse it as an XML file.