
{"id":691,"date":"2009-07-29T03:09:16","date_gmt":"2009-07-29T02:09:16","guid":{"rendered":"http:\/\/dasini.net\/blog\/?p=691"},"modified":"2009-08-10T09:18:28","modified_gmt":"2009-08-10T08:18:28","slug":"utiliser-xml-avec-mysql-5-1-part-25","status":"publish","type":"post","link":"https:\/\/dasini.net\/blog\/2009\/07\/29\/utiliser-xml-avec-mysql-5-1-part-25\/","title":{"rendered":"Utiliser XML avec MySQL 5.1 (part 2\/5)"},"content":{"rendered":"<p><!-- \t\t@page { margin: 2cm } \t\tP { margin-bottom: 0.21cm } \t\tH2 { margin-bottom: 0.21cm } \t\tH2.western { font-family: \"Arial\", sans-serif; font-size: 14pt; font-style: italic } \t\tH2.cjk { font-family: \"MS Mincho\"; font-size: 14pt; font-style: italic } \t\tH2.ctl { font-family: \"Tahoma\"; font-size: 14pt; font-style: italic } --><\/p>\n<p style=\"margin-bottom: 0cm;\">(<a title=\"Utiliser XML avec MySQL 5.1 (part 1\/5)\" href=\"http:\/\/dasini.net\/blog\/2009\/07\/17\/utiliser-xml-avec-mysql-5-1-part-15\/\">&lt;- pr\u00e9c\u00e9dent<\/a>)<\/p>\n<h2>Ins\u00e9rer du XML dans MySQL<\/h2>\n<p style=\"margin-bottom: 0cm;\">Un fichier <em>XML<\/em>, n&rsquo;est finalement qu&rsquo;un fichier contenant du texte et ins\u00e9rer du texte dans un SGBDR, est loin d&rsquo;\u00eatre la chose la plus difficile. Une simple requ\u00eate <span style=\"text-decoration: none;\"><strong>INSERT<\/strong><\/span> fait l&rsquo;affaire. Cela nous donne quelque chose comme ceci :<\/p>\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm; text-decoration: none;\">\n<p style=\"margin-bottom: 0cm; text-decoration: none;\">\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">INSERT INTO ma_table (champ_texte)  VALUES (&lsquo;&lt;?xml version=\u00a0\u00bb1.0&Prime;?&gt;&#8230;&rsquo;)<\/address>\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">Cela dit, si le flux <em>XML<\/em> est important, cette fa\u00e7on de faire n&rsquo;est gu\u00e8re pratique. En fait, la m\u00e9thode la plus simple est d&rsquo;utiliser la fonction <span style=\"text-decoration: none;\">MySQL: <\/span><span style=\"text-decoration: none;\"><strong>LOAD_FILE()<\/strong><\/span><span style=\"text-decoration: none;\">.<\/span><\/p>\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">Cette fonction prend en argument un fichier et renvoie son contenu sous la forme d&rsquo;une cha\u00eene de caract\u00e8res. A noter que vous devez avoir le droit <span style=\"text-decoration: none;\"><strong>FILE<\/strong><\/span> pour ex\u00e9cuter cette commande, et que le fichier doit se trouver sur la machine h\u00f4te (en <em>local<\/em>):<\/p>\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\"><span style=\"text-decoration: underline;\">Ins\u00e9rer un fichier XML dans MySQL<\/span><\/p>\n<p style=\"margin-bottom: 0cm;\">\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql&gt; CREATE DATABASE IF NOT EXISTS xml;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql&gt; USE xml;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql&gt; CREATE TABLE rss (<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">type CHAR(30) NOT NULL,<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">flux_rss TEXT )<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">ENGINE=MyISAM;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql&gt; INSERT INTO xml.rss VALUES (NULL, &lsquo;cinema&rsquo;, LOAD_FILE(&lsquo;.\/cinema.xml&rsquo;));<\/address>\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">Arr\u00eatons nous quelques instants sur la commande <span style=\"text-decoration: none;\"><strong>LOAD XML<\/strong><\/span> qui est apparue avec la version 6 de MySQL (MySQL 6.0.3). Cette commande permet d&rsquo;importer simplement le contenu d&rsquo;un fichier XML dans une table. Elle a une syntaxe assez proche de la commande <span style=\"text-decoration: none;\"><strong>LOAD DATA<\/strong><\/span> que vous connaissez peut-\u00eatre, et qui elle aussi facilite le travail lorsque le besoin d&rsquo;importer des donn\u00e9es d&rsquo;un fichier texte se fait sentir. L&rsquo;exemple suivant nous montre un exemple d&rsquo;utilisation de <span style=\"text-decoration: none;\"><strong>LOAD XML:<\/strong><\/span><\/p>\n<p style=\"margin-bottom: 0cm; text-decoration: none;\">\n<p style=\"margin-bottom: 0cm; text-decoration: none;\">\n<p style=\"margin-bottom: 0cm; text-decoration: none;\">\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\"><span style=\"text-decoration: underline;\">Utilisation de la commande LOAD XML de MySQL 6<\/span><\/p>\n<p style=\"margin-bottom: 0cm;\">\n<p style=\"margin-bottom: 0cm;\">\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql6&gt; USE xml;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql6&gt; CREATE TABLE consultant (<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">id_consultant TINYINT UNSIGNED NOT NULL PRIMARY KEY,<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">nom CHAR(40),<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">prenom CHAR(40)<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">)ENGINE=MyISAM;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql6&gt; LOAD XML LOCAL INFILE &lsquo;consultant .xml&rsquo;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">INTO TABLE  consultant<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">ROWS IDENTIFIED BY &lsquo;&lt;consultant&gt;&rsquo;;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">mysql6&gt; SELECT * FROM consultant;<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">| id_consultant | nom    | prenom          |<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">|             1 | Dasini | Olivier         |<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">|             2 | Allard | Fabien          |<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">|             3 | Pascal  | Borghino          |<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">|             4 | Arnaud  | Gadal |<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"> <\/address>\n<p><!-- --><\/p>\n<p><!-- --><\/p>\n<address style=\"margin-bottom: 0cm; text-decoration: none;\"><em>(<a title=\"Utiliser XML avec MySQL 5.1 (part 3\/5)\" href=\"http:\/\/dasini.net\/blog\/2009\/08\/10\/utiliser-xml-avec-mysql-5-1-part-35\/\">\u00e0 suivre&#8230; utiliser XML avec MySQL 5.1 : Interroger un flux XML avec Xpath<\/a>)<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<\/address>\n","protected":false},"excerpt":{"rendered":"<p>Ins\u00e9rer du XML dans MySQL<\/p>\n<p>Un fichier XML, n&rsquo;est finalement qu&rsquo;un fichier contenant du texte et ins\u00e9rer du texte dans un SGBDR, est loin d&rsquo;\u00eatre la chose la plus difficile. Une simple requ\u00eate INSERT fait l&rsquo;affaire. Cela nous donne quelque chose comme ceci :<\/p>\n<p>INSERT INTO ma_table (champ_texte) VALUES (&lsquo;<?xml version=\"1.0\"?>&#8230;&rsquo;)<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"categories":[8],"tags":[77,78],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-xml","tag-xpath"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9LfWW-b9","jetpack-related-posts":[{"id":673,"url":"https:\/\/dasini.net\/blog\/2009\/07\/17\/utiliser-xml-avec-mysql-5-1-part-15\/","url_meta":{"origin":691,"position":0},"title":"Utiliser XML avec MySQL 5.1 (part 1\/5)","author":"Olivier DASINI","date":"17 juillet 2009","format":false,"excerpt":"MySQL avec son mod\u00e8le relationnel et XML avec son approche hi\u00e9rarchique ont en commun la vocation d'organiser et de structurer des ensembles de donn\u00e9es. En regardant de plus pr\u00e8s, ces technologies peuvent offrir des solutions compl\u00e9mentaires : * MySQL s'adapte \u00e0 merveille \u00e0 la gestion de syst\u00e8mes d'informations \u00e9ventuellement lourds\u2026","rel":"","context":"Dans &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/dasini.net\/blog\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":758,"url":"https:\/\/dasini.net\/blog\/2009\/09\/16\/utiliser-xml-avec-mysql-5-1-part-45\/","url_meta":{"origin":691,"position":1},"title":"Utiliser XML avec MySQL 5.1 (part 4\/5)","author":"Olivier DASINI","date":"16 septembre 2009","format":false,"excerpt":"Les fonctionnalit\u00e9s XPath de MySQL 5 MySQL 5 offre la possibilit\u00e9 de pouvoir ex\u00e9cuter des requ\u00eates XPath sur un flux XML, gr\u00e2ce \u00e0 la fonction extractValue(). Le r\u00e9sultat r\u00e9cup\u00e9r\u00e9 est la concat\u00e9nation de la valeur textuelle de chacun des n\u0153uds trouv\u00e9s (chaque valeur \u00e9tant dissoci\u00e9e des autres par un espace\u2026","rel":"","context":"Dans &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/dasini.net\/blog\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":779,"url":"https:\/\/dasini.net\/blog\/2009\/10\/01\/utiliser-xml-avec-mysql-5-1-part-55\/","url_meta":{"origin":691,"position":2},"title":"Utiliser XML avec MySQL 5.1 (part 5\/5)","author":"Olivier DASINI","date":"1 octobre 2009","format":false,"excerpt":"Voil\u00e0 un petit panorama de l'utilisation des fonctionnalit\u00e9s XML de MySQL. Comme nous l'avons vu, g\u00e9n\u00e9rer le r\u00e9sultat d'une requ\u00eate au format XML reste tr\u00e8s simple avec le client texte mysql, idem pour la g\u00e9n\u00e9ration d'une sauvegarde (mysqldump). De plus, bien que n'\u00e9tant pas un base de donn\u00e9es XML, ont\u2026","rel":"","context":"Dans &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/dasini.net\/blog\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":334,"url":"https:\/\/dasini.net\/blog\/2008\/12\/13\/les-nouveautes-de-mysql-51-part-45\/","url_meta":{"origin":691,"position":3},"title":"Les nouveaut\u00e9s de MySQL 5.1 &#8212; (part 4\/5)","author":"Olivier DASINI","date":"13 d\u00e9cembre 2008","format":false,"excerpt":"Le moteur de stockage CSV n'est pas une vraie nouveaut\u00e9. Il est disponible depuis MySQL 4.1.4 (MySQL 5.1 pour M.S. Windows). Sa particularit\u00e9 est de stocker les donn\u00e9es dans un fichier texte au format CSV (Comma Separated Values) o\u00f9 les donn\u00e9es sont s\u00e9par\u00e9es par une virgule. Les avantages sont multiples,\u2026","rel":"","context":"Dans &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/dasini.net\/blog\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":713,"url":"https:\/\/dasini.net\/blog\/2009\/08\/10\/utiliser-xml-avec-mysql-5-1-part-35\/","url_meta":{"origin":691,"position":4},"title":"Utiliser XML avec MySQL 5.1 (part 3\/5)","author":"Olivier DASINI","date":"10 ao\u00fbt 2009","format":false,"excerpt":"Interroger un flux XML avec Xpath L'emploi de XML ne pr\u00e9senterait pas beaucoup d'int\u00e9r\u00eat sans la possibilit\u00e9 de pouvoir interroger sa structure pour acc\u00e9der \u00e0 une information particuli\u00e8re : le langage XPath r\u00e9pond \u00e0 cette attente et est (en partie du moins) aujourd'hui int\u00e9gr\u00e9 \u00e0 MySQL. Mais avant de voir\u2026","rel":"","context":"Dans &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/dasini.net\/blog\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":606,"url":"https:\/\/dasini.net\/blog\/2009\/04\/30\/le-programmateur-devenements-event-scheduler-part-26\/","url_meta":{"origin":691,"position":5},"title":"Le programmateur d&rsquo;\u00e9v\u00e9nements ( Event Scheduler ) (part 2\/6)","author":"Olivier DASINI","date":"30 avril 2009","format":false,"excerpt":"Cr\u00e9er un \u00e9v\u00e9nement Pour cr\u00e9er notre premier \u00e9v\u00e9nement, un coup d'oeil dans la documentation de MySQL nous donne les informations suivantes:","rel":"","context":"Dans &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/dasini.net\/blog\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/comments?post=691"}],"version-history":[{"count":8,"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":714,"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/posts\/691\/revisions\/714"}],"wp:attachment":[{"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/media?parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/categories?post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dasini.net\/blog\/wp-json\/wp\/v2\/tags?post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}