<?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>Jorge Albaladejo &#187; XML</title>
	<atom:link href="http://jorgealbaladejo.com/category/web-architecture/programming/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://jorgealbaladejo.com</link>
	<description>Hard &#38; Soft design...</description>
	<lastBuildDate>Sat, 10 Dec 2011 16:23:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript 3 and cross-domain problem</title>
		<link>http://jorgealbaladejo.com/2010/09/06/actionscript-and-cross-domain-problem/</link>
		<comments>http://jorgealbaladejo.com/2010/09/06/actionscript-and-cross-domain-problem/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 19:37:27 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Web Security]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[proxy php]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=664</guid>
		<description><![CDATA[One of the most typical problems when coding AS3 to, let&#8217;s say, read data from an external XML source, is the cross-domain problem. According to security policies, not an url outside the domain where the AS application is being executed can be read, and different subdomains (such as www.yourdomain.com and yourdomain.com) are considered as different [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify">One of the most typical problems when coding AS3 to, let&#8217;s say, read data from an external XML source, is the cross-domain problem. According to security policies, not an url outside the domain where the AS application is being executed can be read, and different subdomains (such as www.yourdomain.com and yourdomain.com) are considered as different ones.</p>
<p style="text-align:justify">If the external resources you are trying to use come from your own application in another domain / server that you manage, then it can be fixed by adding a <a href="http://kb2.adobe.com/cps/142/tn_14213.html#main_Cross_domain_policy_files" title="Cross-domain policy files" target="_blank" rel="external">cross-domain policy file</a>. This file must be defined on the content source server, and your AS application has to explicitly <a href="http://stackoverflow.com/questions/1638641/how-do-i-fix-this-cross-domain-actionscript-3-error" title="ActionScript 3 cross-domain problem" target="_blank" rel="external">declare</a> this server as allowed.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6646"><td class="code" id="p664code6"><pre class="actionscript" style="font-family:monospace;">Security.<span style="color: #0066CC;">allowDomain</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;domainone.com&quot;</span><span style="color: #66cc66;">&#41;</span>;
Security.<span style="color: #0066CC;">allowDomain</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;domaintwo.com&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>&nbsp;</p>
<p style="text-align:justify">But if this is not the case and the content source is on a different remote server like, for instance, when implementing an RSS news reader which grabs content from sources like online news sites, then this problem can be worked out with a PHP proxy file.</p>
<p><span id="more-664"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code7'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6647"><td class="code" id="p664code7"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/readfile" rel="external"><span style="color: #990000;">readfile</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p style="text-align:justify">Since PHP is not affected by this policy, all the RSS feeds would be read through this small script, by calling it like &#8216;proxy.php?url=http://rss.thedomain.com&#8217;. Being this file exactly on the same domain than the AS application, the cross-domain problem is solved.</p>
<p style="text-align:justify">However, I have also found an odd behavior regarding this proxy PHP file: it works perfectly when opening the SWF compiled application directly on the browser (http://yourdomain.com/app.swf), but not when it is loaded as a part of the HTML code (embedded into an HTML page like http://yourdomain.com/app.html). The SWF application just doesn&#8217;t find the file (but it <strong>is</strong> there!), despite it reads without any problem a test text file on the same folder. So it seems as if the question mark &#8216;?&#8217; changed the way the SWF application looks for files or executes url addresses. I don&#8217;t have a reason for this yet, but I guess that it is related to the way the Flash plugin manages the security, by using different <a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7c5a.html" target="_blank" title="Security Sandbox in AS3" rel="external">sandboxes</a> for direct and embedded executions of the application.</p>
<p style="text-align:justify">The solution for this second collateral problem is passing the domain name as a parameter, so that the full path to the proxy file can be constructed inside the AS application.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code8'); return false;">View Code</a> HTML4STRICT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6648"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p664code8"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html" rel="external"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html" rel="external"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html" rel="external"><span style="color: #000000; font-weight: bold;">style</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
			...
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html" rel="external"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html" rel="external"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
			...
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html" rel="external"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html" rel="external"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html" rel="external"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html" rel="external"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flash&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;? $basepath <span style="color: #66cc66;">=</span> getBasePath<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> ?&gt;</span>
			<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/object.html" rel="external"><span style="color: #000000; font-weight: bold;">object</span></a> <span style="color: #000066;">classid</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;</span> <span style="color: #000066;">codebase</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://download.macromedia.com/&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;960&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;610&quot;</span>&gt;</span>
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;allowScriptAccess&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;always&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;movie&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main.swf?basepath=&lt;?=$basepath?&gt;</span></span>&quot; /&gt;
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;quality&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;high&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bgcolor&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#ffffff&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
				<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;basepath&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=$basepath?&gt;</span></span>&quot; /&gt;
				<span style="color: #009900;">&lt;embed <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main.swf?basepath=&lt;?=$basepath?&gt;</span></span>&quot; 
				    quality=high bgcolor=#FFFFFF width=&quot;960&quot; height=&quot;610&quot; name=&quot;main&quot;
				    type=&quot;application/x-shockwave-flash&quot; 
				    pluginspace=&quot;http://www.macromedia.com/go/getflashplayer&quot;&gt;
				<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>embed&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/object.html" rel="external"><span style="color: #000000; font-weight: bold;">object</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html" rel="external"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html" rel="external"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html" rel="external"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6649"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p664code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> getBasePath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$aPath</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/split" rel="external"><span style="color: #990000;">split</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/eregi" rel="external"><span style="color: #990000;">eregi</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'\.php'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aPath</span><span style="color: #009900;">&#91;</span><a href="http://www.php.net/sizeof" rel="external"><span style="color: #990000;">sizeof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$aPath</span><span style="color: #009900;">&#91;</span><a href="http://www.php.net/sizeof" rel="external"><span style="color: #990000;">sizeof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/join" rel="external"><span style="color: #990000;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$aPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&nbsp;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66410"><td class="code" id="p664code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> proxyPath:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">loaderInfo</span>.<span style="color: #006600;">parameters</span>.<span style="color: #006600;">basepath</span> + <span style="color: #ff0000;">'proxy.php'</span>; <span style="color: #808080; font-style: italic;">// proxy for cross-domain problems</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p style="text-align:justify">In any case, these problems only arise in online mode, I have not had any issue while working from my development suite, so it is when moving to a production environment when the pain in the ass can show out. Finally, this is nothing new, but handy and interesting not to forget.</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2010/09/06/actionscript-and-cross-domain-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading data in Flash from an external XML</title>
		<link>http://jorgealbaladejo.com/2007/05/28/cargar-datos-en-flash-desde-un-xml-externo/</link>
		<comments>http://jorgealbaladejo.com/2007/05/28/cargar-datos-en-flash-desde-un-xml-externo/#comments</comments>
		<pubDate>Mon, 28 May 2007 11:55:55 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[loadvars]]></category>

		<guid isPermaLink="false">http://labs.abc-webs.net/2007/05/28/cargar-datos-en-flash-desde-un-xml-externo/</guid>
		<description><![CDATA[Notice: Article only available in Spanish! Cuando trabajamos con aplicaciones en flash y queremos que conecten con bases de datos para mostrar catálogos de productos, noticias, etc., la mejor forma de hacerlo es mediante el paso de mensajes en formato XML. Leemos de servicios web correctamente formateados en XML, y después mostraremos los datos según [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Notice</strong>: Article only available in <strong>Spanish</strong>!</p></blockquote>
<p>Cuando trabajamos con aplicaciones en flash y queremos que conecten con bases de datos para mostrar catálogos de productos, noticias, etc., la mejor forma de hacerlo es mediante el paso de mensajes en formato XML. Leemos de servicios web correctamente formateados en XML, y después mostraremos los datos según nos convenga en la aplicación flash.</p>
<p>Partimos de un archivo XML correctamente formateado, bien estático y alojado en cualquier carpeta del servidor, bien una url a una <a href="http://jorgealbaladejo.com/?p=20" title="Formateado de datos de una base de datos en xml mediante php">pasarela php que previamente lea los datos de una base de datos</a> y después los muestre en XML. Vamos a necesitar tres cosas: un objeto LoadVars, un objeto XML y conocer la estructura de los nodos XMLpara poderlos recorrer correctamente.</p>
<p><span id="more-21"></span>LoadVars se usará para enviar una petición http a la url de la pasarela, y XML para recoger los datos que devolverá la petición LoadVars. En nuestro caso, partiendo de la estructura XML mencionada en la publicación arriba enlazada, recorreremos cada nodo (childnode), rescatando sus atributos id, name y special_powers.</p>
<p>El código quedará tal que así­:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2112"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code" id="p21code12"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* script to manage data in flash, retrieved from a XML backend
 * @author Jorge Albaladejo Pomares [correo@jorgealbaladejo.com]
 * @license Creative Commons License http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// variables</span>
<span style="color: #0066CC;">loadvars</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">LoadVars</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
readvars = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
urlxml = <span style="color: #ff0000;">&quot;http://localhost/test.php&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// define data matrixes</span>
readvars.<span style="color: #0066CC;">ignoreWhite</span> = <span style="color: #000000; font-weight: bold;">true</span>;
ids = <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">names</span> = <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
special_powers = <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// execute request and stop main movie execution</span>
<span style="color: #0066CC;">loadvars</span>.<span style="color: #0066CC;">sendAndLoad</span><span style="color: #66cc66;">&#40;</span>urlxml, readvars, <span style="color: #ff0000;">&quot;GET&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//method to be called once data are loaded</span>
readvars.<span style="color: #0066CC;">onLoad</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>success<span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;read correctly&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>success<span style="color: #66cc66;">&#41;</span> 
  <span style="color: #66cc66;">&#123;</span>
    long = readvars.<span style="color: #0066CC;">firstChild</span>.<span style="color: #0066CC;">childNodes</span>.<span style="color: #0066CC;">length</span>;
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>long<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">//Fill array with attributes from each node</span>
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&amp;</span>lt;long; i++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      ids<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = readvars.<span style="color: #0066CC;">firstChild</span>.<span style="color: #0066CC;">childNodes</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">attributes</span>.<span style="color: #006600;">id</span>;
      <span style="color: #0066CC;">names</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = readvars.<span style="color: #0066CC;">firstChild</span>.<span style="color: #0066CC;">childNodes</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">attributes</span>.<span style="color: #0066CC;">name</span>;
      special_powers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = readvars.<span style="color: #0066CC;">firstChild</span>.<span style="color: #0066CC;">childNodes</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">attributes</span>.<span style="color: #006600;">special_powers</span>;
      <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>ids<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> + <span style="color: #ff0000;">' : '</span> + <span style="color: #0066CC;">names</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> + <span style="color: #ff0000;">', '</span> + special_powers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//debug</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span> 
  <span style="color: #b1b100;">else</span> 
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Could not load XML with data!&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>; <span style="color: #808080; font-style: italic;">//onload end of method</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2007/05/28/cargar-datos-en-flash-desde-un-xml-externo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting data in XML with PHP</title>
		<link>http://jorgealbaladejo.com/2007/05/26/formateado-de-datos-en-xml/</link>
		<comments>http://jorgealbaladejo.com/2007/05/26/formateado-de-datos-en-xml/#comments</comments>
		<pubDate>Sat, 26 May 2007 21:32:30 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript]]></category>

		<guid isPermaLink="false">http://labs.abc-webs.net/2007/05/26/formateado-de-datos-en-xml/</guid>
		<description><![CDATA[Notice: Article only available in Spanish! Ahora que ya he explicado cómo crear una clase en php para leer datos de una base de datos, y que también conocemos las bases del lenguaje XML, vamos a crear una clase para formatear estos datos en XML. De este modo, podremos compartir datos entre distintos tipos de [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Notice</strong>: Article only available in <strong>Spanish</strong>!</p></blockquote>
<p>Ahora que ya he explicado <a href="http://jorgealbaladejo.com/?p=19" title="clase dbhandler para manejar datos de la base de datos">cómo crear una clase en php para leer datos</a> de una base de datos, y que también conocemos las bases del lenguaje XML, vamos a crear una clase para formatear estos datos en XML. De este modo, podremos compartir datos entre distintos tipos de aplicaciones, sin necesidad de tener acceso al servidor de bases de datos más que en una de ellas: la pasarela de datos.</p>
<p>Una vez formateamos en correcto XML, podemos ofrecer un documento como XML puro para aplicaciones externas que quieran utilizar nuestros datos, o un RSS estándar para que nuestros visitantes sincronicen las noticias del sitio; podemos también ofrecer un servicio SOAP para aplicaciones que no sólo precisen nuestros datos sino también funciones y servicios avanzados; o podemos simplemente adjuntar una hoja de estilos xls y mostrar nuestro documento en un navegador, como lo haríamos con xhtml y css. Bastante útil, ¿no?<br />
<span id="more-20"></span><br />
<strong>Vamos allá</strong>: para la clase necesitaremos algunas propiedades que permitan almacenar el contenido, así­ como un constructor, un método para cargar datos (en arrays multidimensionales), y otro método para recuperarlos (como arrays). También necesitaremos un método toString(), o toXML más propiamente hablando para formatear directamente los datos a XML. Comencemos con el prototipo de la clase:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2020"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p20code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> xml 
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_version</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_encoding</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$data_label</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$item_label</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * constructor; loads some default values
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Gets data to show in a two dimmensional array and stores it internally
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> setContent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Returns content from a matrix
   * @return the content
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> getContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Shows as a text string values in XML format
   * @return XML formatted data
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Returns XML header
   * @return XML header
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/header" rel="external"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Para la matriz de datos, esperamos un array asociativo, donde cada elemento $content[] tenga el valor asociado al índice, correspondiente al nombre del campo de la tabla. Por ejemplo, en nuestra tabla &#8216;superheroes&#8217; con los campos &#8216;id&#8217;, &#8216;name&#8217;, &#8216;special_powers&#8217;, la matriz que la clase espera obtener es del tipo:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2021"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p20code21"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>             <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;101&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span>            <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;spider man&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'special_powers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;crawling in the walls&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>             <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;102&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span>           <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mosquito man&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'special_powers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;run away from spider man&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">....</span></pre></td></tr></table></div>

<p>y el formato XMLque esperamos de salida es</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code22'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2022"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p20code22"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;101&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;spider man&quot;</span> <span style="color: #000066;">special_powers</span>=<span style="color: #ff0000;">&quot;crawling in the walls&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;102&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mosquito man&quot;</span> <span style="color: #000066;">special_powers</span>=<span style="color: #ff0000;">&quot;run away from spider man&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>podemos elegir varios formatos para el XMLde salida, en concreto usaremos el anterior, sin embargo podríamos haber utilizado un formato anidado del tipo</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code23'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2023"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p20code23"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>101<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spider man<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>crawling in the walls<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>102<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mosquito man<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>run away from spider man<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Elijo el primer formato por sencillez, y porque siendo yo quien voy a leer de él en otras aplicaciones, no tengo que preocuparme por la compatibilidad. Si vamos a integrar nuestra pasarela XML con otros servicios o aplicaciones web, quizá nos interese saber primero qué formato espera encontrar dicha aplicación para ofrecerle datos que le resulten legibles.</p>
<p>Por otra parte, si se usa la <a href="http://jorgealbaladejo.com/?p=19">clase dbhandler</a> para proporcionar los datos en arrays a esta clase, debe modificarse la función readTable para que quede así­</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2024"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p20code24"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * retrieves data from the selected fields of a certain table
 * @param table to read from
 * @param fields to be retrieved
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> readTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//variables</span>
  <span style="color: #000088;">$resultsArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//fields</span>
  <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//remove last comma</span>
  <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr" rel="external"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldsList</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://www.php.net/strlen" rel="external"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldsList</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//consulta</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; FROM &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$table</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resultset</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_query" rel="external"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">identifier</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_num_rows" rel="external"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resultset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_array" rel="external"><span style="color: #990000;">mysql_fetch_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resultset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$resultsArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//return results in a matrix</span>
        <span style="color: #666666; font-style: italic;">// each result has an index called as the column name in their table</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultsArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Sólo queda <strong>rellenar el prototipo</strong> con el contenido de las funciones:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code25'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2025"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td class="code" id="p20code25"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* 
 * class to format data as XML
 * @author Jorge Albaladejo Pomares [correo@jorgealbaladejo.com]
 * @license Creative Commons License http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #000000; font-weight: bold;">class</span> xml 
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_version</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_encoding</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$data_label</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$item_label</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * constructor; loads some default data
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span>       <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_version</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;1.0&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_encoding</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_label</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;datos&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item_label</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Gets data to show in a two dimmensional array
   * and stores it internally
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> setContent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Gets data to show in a two dimensional array
   * and stores it internally
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> getContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Shows as a text string values in XML format
   * @return XML formatted data
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$return</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_label</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//for each element from the matrix, there is a nested matrix with data from mysql.</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item_label</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span> <span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;!--&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">--&gt;</span><span style="color: #004000;">data_label</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Returns XML header
   * @return XML header
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/header" rel="external"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xml_version.&quot;</span>\<span style="color: #0000ff;">&quot; encoding=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_encoding</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>?&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Y un script que utilice estas dos clases (dbhandler y xml) para mostrar datos de una base de datos nos quedaría tan sencillo como ésto:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code26'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2026"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p20code26"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * script connect to a database and read tables using XML
 * @author Jorge Albaladejo Pomares [correo@jorgealbaladejo.com]
 * @license Creative Commons License http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//main classes</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.dbhandler.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.xml.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//main script</span>
<span style="color: #000088;">$db</span>     <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> dbhandler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'john_smith'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'h01y_GRail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pastafarian_food'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fields</span>   <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'special_powers'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">readTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'superheroes'</span><span style="color: #339933;">,</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//got data, show them as XML</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/header" rel="external"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: text/xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2007/05/26/formateado-de-datos-en-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

