Discussion:
how to create a valid xml file with php and mysql
(too old to reply)
Kentor
2006-05-29 04:11:31 UTC
Permalink
I have some info that im grabbing from a mysql database with php and i
want to make an xml file but i know that i need to be carefull with the
& sign and so on... how can i clean the data coming from the db to have
a valid xml file?
Martin Honnen
2006-05-29 12:55:33 UTC
Permalink
Post by Kentor
I have some info that im grabbing from a mysql database with php and i
want to make an xml file but i know that i need to be carefull with the
& sign and so on... how can i clean the data coming from the db to have
a valid xml file?
The latest 5.1.something PHP release has XmlWriter to generate XML:
<www.php.net/XMLWriter>
--
Martin Honnen
http://JavaScript.FAQTs.com/
Kentor
2006-05-29 14:27:53 UTC
Permalink
thanx... it's pretty complex... =/
Kentor
2006-05-30 19:17:20 UTC
Permalink
anything else =/
Tuomas Rannikko
2006-05-30 20:12:48 UTC
Permalink
Post by Kentor
anything else =/
I think you should go with the XMLWriter

However

I guess, if you're just putting some character data in semi-fixed tags
(rather than trying to generate the document structure from the db) you
could maybe escape the character data with something like this:


$original = "this&that ]]><foo";
$pattern = array("&", "<", "]]>");
$replacement = array("&amp;", "&lt;", "]]&gt;");
$escaped = preg_replace($pattern, $replacement, $original);

I didn't test this, and whether this works is up to the implementation
of preg_replace()...

Note if you're planning to put strings into attribute values the ' and "
characters should be escaped as well. (With &apos; and &quot; respectively.)

There is also a caveat; the XML 1.0 spec doesn't allow for some
"special" characters and therefore it is possible that the document is
still not well-formed even if the code does work as I intended. If
you're afraid of encountering characters not allowed to occur in XML 1.0
you should declare the XML version to be 1.1, which allows for almost
all unicode characters. That means you put the string

<?xml version="1.1"?>

first in the document.

--

- Tuomas

Loading...