Question:
Can you write PHP within an XML file?
Alex
2010-07-25 12:54:17 UTC
I want to use PHP to connect to a MySQL database so I can input the values from the database into the XML file. Is this possible?
Four answers:
Rocky
2010-07-25 13:03:54 UTC
it's possilbe,



you treat php as a xml , by echoing the message in xml format



or u can also treat .xml as php file so that when u run the xml file the php code gets executed for that u need to write in htaccess file





ForceType application/x-httpd-php

2016-04-17 11:26:12 UTC
You can't embed PHP in an XML file. Try using the PHP function get_file_contents($url) where the url is a link to the xml file. Then use some php to parse the xml file contents. There are functions to do this.
Frxstrem
2010-07-26 11:37:24 UTC
Yes, there are mainly two ways to do this:



1) If you only need one XML file with PHP in it, just save it as a normal PHP file (.php), but put:

header('Content-type: text/xml');

at the top of the file. This will cause any clients who access the script to handle it as a normal XML file.



2) If you need multiple XML files with PHP, you may want to make PHP handle all .xml files as PHP code. Just add the following lines to the .htaccess file:

RemoveHandler .xml

AddType application/x-httpd-php .php .xml



Now write your PHP code as you normally would, but just be sure to generate an XML file instead of a HTML file. You might want to take a look at SimpleXML [ http://php.net/simplexml ], which is very helpful when it comes to generating XML.
¢σяя вℓιмєу
2010-07-25 13:03:56 UTC
No you cannot. XML is for XML only.



You can do what you want by creating a php file. Also if you go into your MySQL user interface you can export as XML.



If you need your MySQL to write to an XML file on the fly, their are plenty of tutorials online for this if you do a few Google searches.





Oops! Sorry. Maybe Rocky is right and you can. Don't listen to me. ha ha ha Soz


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...