<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Etienne Lachance &#187; php</title>
	<atom:link href="http://etiennelachance.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://etiennelachance.com</link>
	<description></description>
	<lastBuildDate>Sat, 17 Sep 2011 18:39:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Comment générer un fichier XML valide à partir de PHP &#8211; version echo</title>
		<link>http://etiennelachance.com/2009/11/comment-generer-un-fichier-xml-valide-a-partir-de-php-version-echo/</link>
		<comments>http://etiennelachance.com/2009/11/comment-generer-un-fichier-xml-valide-a-partir-de-php-version-echo/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 14:09:19 +0000</pubDate>
		<dc:creator>estheban</dc:creator>
				<category><![CDATA[Tutoriel]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://etiennelachance.com/?p=73</guid>
		<description><![CDATA[Introduction Prenons par exemple que vous générez le tableau suivant, à partir d&#8217;un code PHP, et que vous voulez générer un fichier XML à partir des même données. My CD Collection Title Artist Empire Burlesque Bob Dylan Hide your heart &#8230; <a href="http://etiennelachance.com/2009/11/comment-generer-un-fichier-xml-valide-a-partir-de-php-version-echo/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>

<p>Prenons par exemple que vous générez le tableau suivant, à partir d&#8217;un code PHP, et que vous voulez générer un fichier XML à partir des même données.</p>

<blockquote>
<h2>My CD Collection</h2>
<table border="1">
<tbody>
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
</tr>
<tr>
<td>Greatest Hits</td>
<td>Dolly Parton</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
</tr>
</tbody>
</table>
</blockquote>

<h2>Modification du code</h2>

<p>Voici un exemple de code source :</p>

<blockquote>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// prendre note que le tableau(array) $catalog doit exister</span>
<span style="color: #666666; font-style: italic;">// il peut être généré par programmation ou bien par une base de donnée</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;My CD Collection&lt;/h2&gt;
&lt;table border=&quot;1&quot;&gt;
	&lt;tr bgcolor=&quot;#9acd32&quot;&gt;
		&lt;th&gt;Title&lt;/th&gt;
		&lt;th&gt;Artist&lt;/th&gt;
	&lt;/tr&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$catalog</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cd</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;tr&gt;&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$cd</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$cd</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'artist'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;&lt;/tr&gt;<span style="color: #000000; font-weight: bold;">&lt;?</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>



</blockquote>

<p>Maintenant modifions le code source pour générer un fichier XML :</p>

<blockquote>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// prendre note que le tableau(array) $catalog doit exister</span>
<span style="color: #666666; font-style: italic;">// il peut être généré par programmation ou bien par une base de donnée</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
header (&quot;Content-Type:text/xml&quot;);			// spécifie à php d'envoyer l'en-tête XML
echo '<span style="color: #000000; font-weight: bold;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>';	// voir plus bas
&lt;catalog&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$catalog</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cd</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;cd&gt;
		&lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$cd</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;
		&lt;artist&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$cd</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'artist'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/artist&gt;
	&lt;/cd&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/catalog&gt;</pre></div></div>



</blockquote>

<p>Faire attention à ligne <strong>echo &#8216;<?xml version="1.0" encoding="ISO-8859-1"?>&#8216;; </strong>
Ne pas utiliser la syntax :</p>

<blockquote>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// code</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-9959-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">// code suite</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>



</blockquote>

<p>Parce que cela va générer une erreur PHP.</p>

<h2>Résultat</h2>

<p>Voici finalement la résultante du script :</p>

<blockquote>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;?<span style="color: #000066;">xmlversion</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catalog<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Empire Burlesque<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bob Dylan<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Hide your heart<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bonnie Tyler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Greatest Hits<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Dolly Parton<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Still got the blues<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Gary Moore<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/catalog<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>



</blockquote>

<h2>Référence</h2>

<ul>
    <li><a href="http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&amp;xsltfile=cdcatalog">http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&amp;xsltfile=cdcatalog</a></li>
    <li><a href="http://www.php.net/manual/en/simplexml.examples-basic.php">http://www.php.net/manual/en/simplexml.examples-basic.php</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://etiennelachance.com/2009/11/comment-generer-un-fichier-xml-valide-a-partir-de-php-version-echo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

