I am using this code to take data from the sql database and convert it into an xml file. However some of the row results from the mysql database have characters that cause errors with the xml file and it doesn't close the elements. For example if there is an apostrophe or euro sign. I thought the using writecdata would sort it but it still failed (also is the resulting xml meant to show the element as <![CDATA[ name ]]> ?)

You may need to wrap your data in utf8_encode(); Your server may not be using UTF-8 as its standard charset.

SQHell
—
2013-12-02T19:34:03Z —
#3

Thanks for the reply, I have removed the space but its made no difference. How do I wrap the data in utf8_encode?

StarLion
—
2013-12-02T19:35:06Z —
#4

quite literally.utf8_encode($name);

SQHell
—
2013-12-03T20:19:55Z —
#5

Yay, that's it working now, thanks very much

Is there a need for Cdata? My results are coming out like this

<Name> <![CDATA[ Dunroamin ]]></Name>

Which doesn't seem right or is that valid XML?

SQHell
—
2013-12-03T20:27:04Z —
#6

This may sound like a stupid question, while I am using php to create the xml output, what should the file extension be? php or xml? What if I wanted it as xml?

KyleWolfe
—
2013-12-03T20:54:30Z —
#7

Your file extension is php, its processing and running in PHP. Your header() function takes care of making sure that whoever is reading your page, knows its being sent in XML.

Did you not like SimpleXMLElement and DOM? I've never heard of XMLWriter

SQHell
—
2013-12-03T21:20:46Z —
#8

Hi,

Its just an example I found and I have adapted it to work.

I have answered my own question regarding making an xml file by used fopen, fwrite and fclose. I take it to keep the xml file up to date I would need to set up a cron job to run the script?

Thanks

KyleWolfe
—
2013-12-03T21:31:36Z —
#9

SQHell said:

Hi,

Its just an example I found and I have adapted it to work.

I have answered my own question regarding making an xml file by used fopen, fwrite and fclose. I take it to keep the xml file up to date I would need to set up a cron job to run the script?

Thanks

I think you've got the wrong idea. You can server the xml file up dynamically, you should never have to write to a file and then read from it again UNLESS you want to prevent a db connection on every connection and update the data on an interval of your choosing, through a cron job.

If you want to serve dynamically, it should be pretty straight forward:

Just looking at your example, surely if I wanted to see the result of that script I would go to the url with an php extension eg mysqltoxml.php but what if I needed mysqltoxml.xml though?

KyleWolfe
—
2013-12-03T22:20:41Z —
#12

Apparently I exited my tab without posting my response. Let's go roll back to what you are actually looking to accomplish. Are you trying to load a page and it gives you a physical xml file? Where are you hoping to retrieve this xml payload from? Normally, the client (where you are retrieving the xml from) will do a curl call and parse it from there.

SQHell
—
2013-12-05T20:40:10Z —
#13

Right, I need to provide an xml file with an .xml extension to another company so that the properties can be displayed on their website.

KyleWolfe
—
2013-12-05T21:44:01Z —
#14

SQHell said:

Right, I need to provide an xml file with an .xml extension to another company so that the properties can be displayed on their website.

right, setting the header to text/xml does this, it just happens that your browser opens it up to be viewed in your browser. You can save the web page and it will automatically be an xml file. Exactly the same as when you open a PDF online. You know its a PDF, its just that the browser is handling it for you and you can save the pdf from there if you want.