{"id":5,"date":"2004-10-29T20:47:23","date_gmt":"2004-10-29T20:47:23","guid":{"rendered":"http:\/\/gaisan.com\/wordp\/?p=5"},"modified":"2004-10-29T20:47:23","modified_gmt":"2004-10-29T20:47:23","slug":"mysql-xml-output","status":"publish","type":"post","link":"https:\/\/gaisan.com\/blogs\/?p=5","title":{"rendered":"MySql &#038; XML Output"},"content":{"rendered":"<p>Pulled <a href=\"http:\/\/www.decafbad.com\/blog\/2004\/08\/23\/mysql_and_xml_output\"  title=\"Link outside of this blog\">this from DECAFBAD<\/a>&#8230; Nice tasty article all about <code>-X<\/code> command line switch in mysql. Wonderful I thought. XML based power in the worlds most popular <b>and free<\/b> relational database. I heard the sounds of dreams coming true.. well maybe \ud83d\ude09 There had to be a catch however.<br \/>\n<br \/>However, just when I thought everything was peachy I started playing with this feature with less than spectacular results. Some intensive googling yielded <a href=\"http:\/\/dev.mysql.com\/doc\/mysql\/en\/mysql.html\">the answer.<\/a><br \/>\nIt seems that using the -X command line option for exporting the data in XML format produces invalid XML. <b>It assumes XML escaped data in the DB!.. On what grounds???<\/b> Mysql only encloses the query results in XML element tags, but doesn&#8217;t do encoding of the contents inside the tags.<br \/>\n<br \/>\nIn XML, if you want to use one of the characters &lt;, &gt;, &amp;, etc. inside an element tag is not valid. If you want to use one of those characters, you have to use the respective entity instead. Mysql doesn&#8217;t seem to do that, so when selecting tagged data or markup like &#8220;&lt;foo&gt;red &amp; green&lt;&#038;#47foo&gt;&#8221; with the -X command line option will always lead to invalid XML.<br \/>\nAn uncool workaround would be to perform some string replacements for every selected column when using the -X option:<\/p>\n<ul>\n<li>replace all &#038; by &amp;amp;<\/li>\n<li>replace all &lt; by &amp;lt;<\/li>\n<li>replace all &gt; by &amp;gt;<\/li>\n<li>replace all &quot; by &amp;quot;<\/li>\n<li>replace all &#39; by &amp;#39;<\/li>\n<\/ul>\n<p>Other stuff, like language specific characters (umlauts etc.) has to be encoded as well or has to be handled by defining or applying a different character set when post processing the XML output.<br \/>\n<br \/>So the command produces invalid XML as invalid chars haven&#8217;t been escaped&#8230; Now this is a shame as writing some code to escape it in the db server coulda been done quite easily. A combination of escapes and using different charsets (perhaps as a command line option) along the lines of <code>mysql --xml --xmlcharset=<i>mycharset<\/i><\/code> would be sweet. We&#8217;ll see what happens in the next release<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pulled this from DECAFBAD&#8230; Nice tasty article all about -X command line switch in mysql. Wonderful I thought. XML based power in the worlds most popular and free relational database. I heard the sounds of dreams coming true.. well maybe \ud83d\ude09 There had to be a catch however. However, just when I thought everything was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/5"}],"collection":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5"}],"version-history":[{"count":0,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions"}],"wp:attachment":[{"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaisan.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}