<?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>Jon Segador</title>
	<atom:link href="http://jonsegador.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonsegador.com</link>
	<description>Desarrollador web y android, con todo lo que ello implica</description>
	<lastBuildDate>Wed, 16 May 2012 08:34:41 +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>Selects anidados utilizando el atributo DATA de HTML5</title>
		<link>http://jonsegador.com/2012/05/selects-anidados-utilizando-el-atributo-data-de-html5/</link>
		<comments>http://jonsegador.com/2012/05/selects-anidados-utilizando-el-atributo-data-de-html5/#comments</comments>
		<pubDate>Wed, 16 May 2012 08:34:41 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[anidado]]></category>
		<category><![CDATA[atributos data]]></category>
		<category><![CDATA[desplegable]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=890</guid>
		<description><![CDATA[Vamos a hacer uso del atributo DATA de HTML5 para crear dos selects anidados. En algunas ocasiones necesitamos saber que tipo de dato hemos seleccionado en el primer desplegable en lugar del valor en sí. Podría darse el caso de un primer select con razas de animales y el segundo (que cambiará según lo que [...]]]></description>
			<content:encoded><![CDATA[<p>Vamos a hacer uso del <strong>atributo DATA de HTML5</strong> para crear dos <strong>selects anidados</strong>. En algunas ocasiones necesitamos saber que tipo de dato hemos seleccionado en el primer desplegable en lugar del valor en sí. Podría darse el caso de un primer select con razas de animales  y el segundo (que cambiará según lo que hemos seleccionado en el primero) con comida para diferentes tipos de animal.</p>
<p>Creo que es más claro con un ejemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;select id=&quot;animales&quot;&gt;
  &lt;option value=&quot;1&quot; data-type=&quot;perro&quot;&gt;Caniche&lt;/option&gt;
  &lt;option value=&quot;2&quot; data-type=&quot;gato&quot;&gt;Siamés&lt;/option&gt;
  &lt;option value=&quot;3&quot; data-type=&quot;gato&quot;&gt;Persa&lt;/option&gt;
  &lt;option value=&quot;3&quot; data-type=&quot;perro&quot;&gt;Pastor alemán&lt;/option&gt;
&lt;/select&gt;</pre></td></tr></table></div>

<p>En el segundo despegable mostraremos los diferentes tipos de comida según si hemos seleccionado un perro o un gato. En este caso nos da igual la raza, solo queremos saber que tipo de animal es.</p>
<p>Como se puede ver, he añadido el atributo <strong>data-type</strong> a cada option del select con el tipo de animal que es. Este <strong>valor personalizado</strong> lo podemos obtener fácilmente con <strong>jQuery</strong> tal que así:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'animales'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #003366; font-weight: bold;">var</span> seleccionado <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'option:selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #006600; font-style: italic;">// 'type' es lo que va a continuación del guión en data-type</span>
       <span style="color: #003366; font-weight: bold;">var</span> animal <span style="color: #339933;">=</span> seleccionado.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       ...
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>También podemos acceder a este valor con <strong>Javascript básico</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> animales <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'animales'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> seleccionado <span style="color: #339933;">=</span> animales.<span style="color: #660066;">options</span><span style="color: #009900;">&#91;</span>animales.<span style="color: #660066;">selectedIndex</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> animal <span style="color: #339933;">=</span> seleccionado.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Ahora ya podremos generar el segundo desplegable con los diferentes tipos de comida según el tipo de animal que hemos seleccionado.</p>
<p>Más información sobre los <strong>atributos DATA de HTML5</strong>: <a href="http://html5doctor.com/html5-custom-data-attributes/">HTML5 Custom Data Attributes (data-*)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/05/selects-anidados-utilizando-el-atributo-data-de-html5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Motiva y no te aproveches de tus programadores</title>
		<link>http://jonsegador.com/2012/05/motiva-y-no-te-aproveches-de-tus-programadores/</link>
		<comments>http://jonsegador.com/2012/05/motiva-y-no-te-aproveches-de-tus-programadores/#comments</comments>
		<pubDate>Mon, 14 May 2012 17:55:06 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Opinión personal]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[motivación]]></category>
		<category><![CDATA[programadores]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=853</guid>
		<description><![CDATA[A los programadores no les gusta que se aprovechen de ellos así que no lo hagas. A pesar de los numerosos chistes y opinión generalizada de que no tenemos vida social, ni novia, etc. en la mayoría de los casos es mentira. Es cierto que pasamos muchas horas en frente del ordenador, sin contar las [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A los programadores no les gusta que se aprovechen de ellos</strong> así que no lo hagas. A pesar de los numerosos chistes y opinión generalizada de que no tenemos vida social, ni novia, etc. en la mayoría de los casos es mentira. Es cierto que <strong>pasamos muchas horas en frente del ordenador</strong>, sin contar las 8 horas que estamos en la oficina. Es algo que va con nosotros y con nuestro <strong>trabajo/pasión</strong> si queremos progresar.</p>
<p><strong>Nuestro objetivo</strong> cuando llegamos a casa y hemos terminado de trabajar para la empresa, es aprender o <strong>profundizar</strong> en nuevos temas que nos llaman la atención y <strong>aprender</strong>: un nuevo lenguaje de programación, esa nueva herramienta que ha visto la luz, algún proyecto personal, etc. Pero no nos gusta que se aprovechen de nosotros en base a nuestras inquietudes. Si quieres que tu equipo técnico trabaje con una determinada herramienta o framework, quieres que las webs se maqueten en html5, etc. no te aproveches del programador de turno que se ha interesado en su tiempo libre por ello. <strong>¿Por qué no dedicar una hora al día en aprenderlo en horario laboral?</strong></p>
<p>Cuando me compré un móvil con <a href="http://jonsegador.com/category/android/">Android</a> (mi querida HTC Magic) <strong>no tardé ni dos días en investigar y aprender a programar para ella</strong>. No sabía mucho JAVA, así que en menos de dos meses me leí dos libros sobre JAVA y realicé muchos tutoriales y manuales de Android. Logré desarrollar alguna aplicación para Android como la de <a href="https://github.com/jonseg/cercanias-renfe-android">Cercanías Renfe</a>. Todo esto por mi cuenta y en mi tiempo libre, privándome de otras cosas o actividades de ocio (que sí, que las tenemos). Mi empresa por aquel entonces se <em>aprovechó</em> de esta circustancia y se decidió programar una aplicación para Android. <strong>No había dedicado ni un solo segundo en horario laboral a aprender a programar para Android</strong> y jamás recibí un &#8220;gracias&#8221;, alguna palabra de motivación o beneficio (y no todo es dinero). Todo lo contrario, en una ocasión tuve que escuchar que la aplicación se había programado en Android y no en iPhone por el simple hecho de que para Android era mucho más fácil. <strong>Mentira&#8230;</strong> Si me hubiese comprado un iPhone y me hubiese puesto a programar para él (en mi tiempo libre, insisto), la app se hubiese desarrollado para iPhone. Luego dirán que no hay talento, que la gente no quiere trabajar, que los empleados no se comprometen con la empresa, etc.</p>
<p>¿A qué viene ahora todo esto? En <a href="http://www.perception.es/">mi empresa actual</a> hay especial <strong>interés en desarrollar aplicaciones para iPhone</strong>. ¿Resultado? Me dan un MacBook Air en <a href="http://es.wikipedia.org/wiki/Usufructo">usufructo</a> y yo <strong>aprendo en mi tiempo libre</strong> (además de una formación). Yo encantado (como loco por la oportunidad de aprender algo nuevo) ya que de otra forma no podría hacerlo (no me puedo permitir comprar un Mac) y la empresa más de lo mismo ya que sacará beneficio de ello, siendo un nuevo producto que se puede ofrecer a los clientes. <strong>El programador también gana en confianza y se siente más integrado en la empresa</strong>, sabiendo que se valora su trabajo y su esfuerzo no solamente durante las horas de oficina. Yo no tendría dudas en intentar proponer todas las cosas nuevas que aprenda (siempre que merezcan la pena), así como sugerir proyectos, etc.</p>
<p>En realidad <strong>es muy fácil hacer las cosas bien</strong> y valorar el trabajo de tus programadores. No somos &#8220;monos picateclas&#8221; que nos pueden sustituir por otro &#8220;mono&#8221;. Si la base de tu empresa es tecnologíca, <strong>no puedes descuidar a tus programadores</strong>. Después es muy fácil quejarse.</p>
<p><strong>Espero pronto decir que soy un iPhone developer</strong> <img src='http://jonsegador.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  También supongo que escribiré algún post sobre mis comienzos con esta plataforma y el lenguaje de programación Objective-C con la idea de que le sea de utilidad a alguien.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/05/motiva-y-no-te-aproveches-de-tus-programadores/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Desplegable con una relación entre entities en un formulario de Symfony2</title>
		<link>http://jonsegador.com/2012/05/desplegable-select-relacion-entre-entities-formulario-symfony2/</link>
		<comments>http://jonsegador.com/2012/05/desplegable-select-relacion-entre-entities-formulario-symfony2/#comments</comments>
		<pubDate>Mon, 07 May 2012 14:32:34 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[AbstractType]]></category>
		<category><![CDATA[entity]]></category>
		<category><![CDATA[formularios]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=840</guid>
		<description><![CDATA[En las entidades de Symfony2 es sencillo relacionar un campo con otra tabla con una relación ManyToOne y que este campo solo pueda tomar un valor que esté disponible en la segunda entity. Si creamos un formulario para poder introducir nuevos registros en la base de datos, necesitamos un campo del tipo select para seleccionar [...]]]></description>
			<content:encoded><![CDATA[<p>En las entidades de <a href="http://jonsegador.com/category/symfony2/">Symfony2</a> es sencillo relacionar un campo con otra tabla con una <strong>relación ManyToOne</strong> y que este campo solo pueda tomar un valor que esté disponible en la segunda entity. Si creamos un <a href="http://jonsegador.com/tag/formularios/">formulario</a> para poder introducir nuevos registros en la base de datos, necesitamos un <strong>campo del tipo select</strong> para seleccionar uno de los datos de la segunda tabla y relacionarlos.</p>
<p>Deberemos modificar el <strong>formulario</strong> y añadirle un par de opciones más. En este ejemplo, vamos a relacionar un usuario con su país.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">namespace</span> Acme\MiBundle\Form<span style="color: #339933;">;</span>
use Symfony\Component\Form\AbstractType<span style="color: #339933;">;</span>
use Symfony\Component\Form\FormBuilder<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> PerfilType <span style="color: #000000; font-weight: bold;">extends</span> AbstractType <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildForm<span style="color: #009900;">&#40;</span>FormBuilder <span style="color: #000088;">$builder</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pais'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'entity'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'AcmeMiBundle:Pais'</span>
                <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Pais'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Que no se nos olvide añadir el método <strong>__toString()</strong> a la entity Pais:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">namespace</span> Acme\MiBundle\Entity<span style="color: #339933;">;</span>
&nbsp;
use Doctrine\ORM\Mapping <span style="color: #b1b100;">as</span> ORM<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Pais
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNombre</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Y así de sencillo, ahora en nuestra plantilla aparecerá automáticamente el <strong>campo de tipo select</strong> a la hora de seleccionar el país del usuario.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/05/desplegable-select-relacion-entre-entities-formulario-symfony2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error con la función indexOf() al utilizarla con arrays en Internet Explorer</title>
		<link>http://jonsegador.com/2012/03/error-funcion-indexof-array-internet-explorer/</link>
		<comments>http://jonsegador.com/2012/03/error-funcion-indexof-array-internet-explorer/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 16:05:25 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[explorer]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[indexOf]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=829</guid>
		<description><![CDATA[Trabajando con arrays en javascript, una función bastante interesante con la que contamos es con indexOf() para obtener la posición de la primera ocurrencia del valor especificado en un array. Casualmente, esta función no está disponible para arrays en Internet Explorer 8 y anteriores pero si que está disponible para utilizar con strings. Con este [...]]]></description>
			<content:encoded><![CDATA[<p>Trabajando con <strong>arrays en javascript</strong>, una función bastante interesante con la que contamos es con <a href="http://www.w3schools.com/jsref/jsref_indexof_array.asp"><strong>indexOf()</strong></a> para obtener la posición de la primera ocurrencia del valor especificado en un array.</p>
<p>Casualmente, esta función no está disponible para arrays en <strong>Internet Explorer 8</strong> y anteriores pero si que está disponible para utilizar con <a href="http://www.w3schools.com/jsref/jsref_indexof.asp">strings</a>.</p>
<p>Con este sencillo <em>&#8220;hack&#8221;</em> podremos contar con esta función cuando manejemos arrays.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Array.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">indexOf</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>this.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">return</span> i<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/03/error-funcion-indexof-array-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error con Symfony2: You cannot refresh a user from the EntityUserProvider that does not contain an identifier</title>
		<link>http://jonsegador.com/2012/03/error-con-symfony2-you-cannot-refresh-a-user-from-the-entityuserprovider-that-does-not-contain-an-identifier/</link>
		<comments>http://jonsegador.com/2012/03/error-con-symfony2-you-cannot-refresh-a-user-from-the-entityuserprovider-that-does-not-contain-an-identifier/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 13:12:25 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[doctrine2]]></category>
		<category><![CDATA[EntityUserProvider]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[serializable]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[unserialize]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=808</guid>
		<description><![CDATA[He creado dos bundles, uno llamado UserBundle y otro ProfileBundle. El bundle UserBundle es totalmente independiente y reutilizable en cualquier proyecto (contando con funcionalidades tales como registro, login, logout, recordar contraseña, confirmar usuario mediante email, etc.) con la única pega de que solamente se guardan el email y la contraseña del usuario. Hay proyectos que [...]]]></description>
			<content:encoded><![CDATA[<p>He creado dos <a href="http://jonsegador.com/tag/bundle/">bundles</a>, uno llamado UserBundle y otro ProfileBundle. <strong>El bundle UserBundle es totalmente independiente y reutilizable en cualquier proyecto</strong> (contando con funcionalidades tales como registro, login, logout, recordar contraseña, confirmar usuario mediante email, etc.) con la única pega de que solamente se guardan el email y la contraseña del usuario. <strong>Hay proyectos que solo requieren estos datos</strong> y no merece la pena &#8220;ensuciarlo&#8221; con más.</p>
<p><strong>El bundle ProfileBundle subsana esta limitación</strong> y otorga al usuario un perfil con su nombre, apellidos, etc. El registro se hace desde el UserBundle y, una vez logueado en la aplicación, completas tu perfil de usuario mediante el ProfileBundle.</p>
<p>Al terminar de completar tu perfil y pulsar el botón <em>guardar</em>, el usuario logueado se recarga con los nuevos datos introducidos. Al hacer esto, me aparecía el siguiente error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine.</pre></div></div>

<p>Y en otras ocasiones también se mostraba este otro error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL</pre></div></div>

<p>La solución es sencilla y solo hay que fijarse en el mensaje de error. Si el usuario se va a recargar una vez ya está logueado, la entidad Usuario debe <strong>implementar la clase Serializable y definir sus métodos serialize() y unserialize()</strong>.</p>
<p>En diferentes blogs que hablan sobre <a href="http://jonsegador.com/category/symfony2/">Symfony2</a>, en los métodos serialize y unserialize hacen uso del método getUsername() lo cual <strong>puede ser un error</strong>. Lo muestro con un ejemplo:</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User implements UserInterface<span style="color: #339933;">,</span> \Serializable
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var integer $id
     *
     * @ORM\Column(name=&quot;id&quot;, type=&quot;integer&quot;)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy=&quot;AUTO&quot;)
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string $email
     *
     * @ORM\Column(name=&quot;email&quot;, type=&quot;string&quot;, length=255, unique=true)
     * @Assert\Email()
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> getUsername<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">serialize</span><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: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUsername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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;">email</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>En el ejemplo anterior vemos que <strong>la propiedad $email no es la clave primaria</strong> y, aunque hayamos marcado que es única, nos seguirá apareciendo el error anterior ya que Doctrine necesita que el refresh del usuario se haga mediante su identificador único (en algún caso podría llegar a ser el email, pero no en este ejemplo).</p>
<p>Solamente tendremos que cambiar el código anterior por este:</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User implements UserInterface<span style="color: #339933;">,</span> \Serializable
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var integer $id
     *
     * @ORM\Column(name=&quot;id&quot;, type=&quot;integer&quot;)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy=&quot;AUTO&quot;)
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string $email
     *
     * @ORM\Column(name=&quot;email&quot;, type=&quot;string&quot;, length=255, unique=true)
     * @Assert\Email()
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> getUsername<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">serialize</span><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: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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;">id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>El código anterior se puede optimizar</strong> y se podrían añadir más propiedades a los métodos serialize y unserialize (se podrían utilizar en conjunto las propiedades id, email, etc.).</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/03/error-con-symfony2-you-cannot-refresh-a-user-from-the-entityuserprovider-that-does-not-contain-an-identifier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Symfony2: Añadir a un formulario un campo que no está definido en el modelo</title>
		<link>http://jonsegador.com/2012/02/symfony2-anadir-formulario-un-campo-que-no-esta-definido-modelo/</link>
		<comments>http://jonsegador.com/2012/02/symfony2-anadir-formulario-un-campo-que-no-esta-definido-modelo/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 09:21:23 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[addValidator]]></category>
		<category><![CDATA[buildForm]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[CallbackValidator]]></category>
		<category><![CDATA[formularios]]></category>
		<category><![CDATA[property_path]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=800</guid>
		<description><![CDATA[He estado desarrollando mi propio bundle para el registro de usuarios en Symfony2 y me he encontrado con el problema de querer añadir campos al formulario de registro que no he definido en el modelo. Uno de estos campos es un checkbox para aceptar los términos de uso de la aplicación. No me interesa guardar [...]]]></description>
			<content:encoded><![CDATA[<p>He estado desarrollando mi propio <strong>bundle para el registro de usuarios en Symfony2</strong> y me he encontrado con el problema de querer <strong>añadir campos al formulario de registro que no he definido en el modelo</strong>. Uno de estos campos es un checkbox para aceptar los términos de uso de la aplicación. No me interesa guardar este campo en la base de datos por lo que no está definido en el modelo.</p>
<p>Así es como lo he hecho:</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> RegisterFormType <span style="color: #000000; font-weight: bold;">extends</span> AbstractType
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildForm<span style="color: #009900;">&#40;</span>FormBuilder <span style="color: #000088;">$builder</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$builder</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'repeated'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'invalid_message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Las dos contraseñas deben coincidir'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Contraseña'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accept_tos&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;checkbox&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">&quot;property_path&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #000088;">$builder</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CallbackValidator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>FormInterface <span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;accept_tos&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addError</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FormError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Debes aceptar los términos de uso'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Como vemos, añadimos un campo checkbox con nombre &#8220;accept_tos&#8221; y le indicamos la <strong>opción &#8220;property_path&#8221; a false</strong>, por lo que no validará este campo con el modelo. Eso sí, debemos añadir un validador para este campo para asegurarnos de que el usuario acepta las condiciones de uso.</p>
<p>Fuente: <a href="http://www.richsage.co.uk/2011/07/20/adding-non-entity-fields-to-your-symfony2-forms/">http://www.richsage.co.uk/2011/07/20/adding-non-entity-fields-to-your-symfony2-forms/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/02/symfony2-anadir-formulario-un-campo-que-no-esta-definido-modelo/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Leer/parsear archivo XML en Android mediante SAX</title>
		<link>http://jonsegador.com/2012/02/leer-parsear-archivo-xml-en-android-mediante-sax/</link>
		<comments>http://jonsegador.com/2012/02/leer-parsear-archivo-xml-en-android-mediante-sax/#comments</comments>
		<pubDate>Sun, 19 Feb 2012 15:56:45 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[ideas4all]]></category>
		<category><![CDATA[leer]]></category>
		<category><![CDATA[parsear]]></category>
		<category><![CDATA[progressdialog]]></category>
		<category><![CDATA[sax]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=781</guid>
		<description><![CDATA[Con motivo del concurso #BigBangApps convocado por la web Ideas4All en que proponían desarrollar una aplicación utilizando su API, me puse a programar una aplicación para Android de la web. La API de Ideas4All devuelve los datos en XML (hubiese preferido en JSON pero de momento no lo permite) por lo que el &#8220;gran secreto&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Con motivo del concurso #BigBangApps convocado por la web <a href="http://www.ideas4all.com/">Ideas4All</a> en que proponían desarrollar una aplicación utilizando su API, me puse a programar una aplicación para <a href="http://jonsegador.com/category/android/">Android</a> de la web. <strong>La API de Ideas4All</strong> devuelve los datos en XML (hubiese preferido en <strong>JSON</strong> pero de momento no lo permite) por lo que el &#8220;gran secreto&#8221; de la app es leer correctamente este tipo de archivos.</p>
<p>Hay varias <strong>formas de leer archivos XML</strong>, siendo una de ellas mediante <strong>SAX</strong> (Simple API for XML). En este post voy a explicar los pasos para leer/parsear correctamente un archivo XML disponible en Internet.</p>
<p>El fichero de ejemplo que queremos leer es el listado de categorías:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;categories</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;array&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category<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> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>6<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>Vida/Salud<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;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category<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> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<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>Sostenibilidad<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;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/categories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Lo primero que necesitamos es una clase que utilizaremos para guardar los datos de cada categoría. El objetivo es devolver una lista de objetos del tipo categoría, donde se encontraran todas las categorías leídas del XML.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ParsedCategoryDataSet <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> extractedString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> extractedString<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>    
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> extractedString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> extractedString<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;name = &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Como podemos observar, solamente se trata de una clase con setters y getters para almacenar los datos de cada categoría.</p>
<p>Ahora pasamos a crear la <strong>clase con la que parsearemos el archivo XML mediante SAX en Android</strong>.<br />
El modelo <strong>SAX</strong> lee secuencialmente el fichero XML y ejecuta varios métodos (que podemos controlar) por cada elemento leído. Estos métodos son:</p>
<p>- <strong>startDocument()</strong>: comienza a leer el XML.<br />
- <strong>endDocument()</strong>: ha terminado de leer XML.<br />
- <strong>startElement()</strong>: ha encontrado el comienzo de una etiqueta XML.<br />
- <strong>endElement()</strong>: ha encontrado el cierre de una etiqueta XML.<br />
- <strong>characters()</strong>: texto que ha encontrado entre el comienzo y el cierre de una etiqueta</p>
<p>Estos métodos se encuentran en la clase <em>org.xml.sax.helpers.DefaultHandler</em>, de la que heredaremos nuestro propio parseador de categorías:</p>

<div class="wp_syntax"><table><tr><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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Vector</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.Attributes</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.SAXException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.helpers.DefaultHandler</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CategoryHandler <span style="color: #000000; font-weight: bold;">extends</span> DefaultHandler<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> CategoryHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myParsedCategoryDataSet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>ParsedCategoryDataSet<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>	
&nbsp;
    <span style="color: #666666; font-style: italic;">// Variables de control para saber cuando estamos en el interior de cada etiqueta</span>
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unused&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> in_category <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> in_id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> in_name <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// En esta variable guardamos el texto encontrado entre las etiquetas</span>
    StringBuilder builder<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Aquí guardamos cada objeto categoria</span>
    <span style="color: #000000; font-weight: bold;">private</span> ParsedCategoryDataSet DataSet<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Vector donde se guardaran todas las categorías encontradas</span>
    <span style="color: #000000; font-weight: bold;">private</span> Vector<span style="color: #339933;">&lt;</span>ParsedCategoryDataSet<span style="color: #339933;">&gt;</span> myParsedCategoryDataSet<span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Vector<span style="color: #339933;">&lt;</span>ParsedCategoryDataSet<span style="color: #339933;">&gt;</span> getParsedCategoryDataSets<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myParsedCategoryDataSet</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>      
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Vector<span style="color: #339933;">&lt;</span>ParsedCategoryDataSet<span style="color: #339933;">&gt;</span> getParsedData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myParsedCategoryDataSet</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> startDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SAXException <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// Comenzamos a leer el fichero xml, creamos el vector donde se guardarán las categorías</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myParsedCategoryDataSet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>ParsedCategoryDataSet<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> endDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SAXException <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// Ha terminado de leer el fichero, en este paso no hacemos nada</span>
    <span style="color: #009900;">&#125;</span>     
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> startElement<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> namespaceURI, <span style="color: #003399;">String</span> localName,
              <span style="color: #003399;">String</span> qName, <span style="color: #003399;">Attributes</span> atts<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SAXException <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>localName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;category&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
             <span style="color: #666666; font-style: italic;">// Ha encontrado la etiqueta principal de cada elemento &quot;category&quot;</span>
             <span style="color: #666666; font-style: italic;">// Creamos un nuevo objeto categoría donde iremos guardando los datos</span>
             <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_category</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
             DataSet <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParsedCategoryDataSet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>localName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
             <span style="color: #666666; font-style: italic;">// Estamos dentro de la etiqueta &quot;id&quot;, creamos el StringBuilder que utilizaremos</span>
             <span style="color: #666666; font-style: italic;">// en el método characters para guardar el contenido</span>
             <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_id</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
             builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>localName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
             <span style="color: #666666; font-style: italic;">// Estamos dentro de la etiqueta &quot;name&quot;, creamos el StringBuilder que utilizaremos</span>
             <span style="color: #666666; font-style: italic;">// en el método characters para guardar el contenido</span>
             <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_name</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
             builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> 
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> endElement<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> namespaceURI, <span style="color: #003399;">String</span> localName, <span style="color: #003399;">String</span> qName<span style="color: #009900;">&#41;</span>
              <span style="color: #000000; font-weight: bold;">throws</span> SAXException <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>localName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;category&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Hemos llegado al final de la etiqueta principal de cada elemento &quot;category&quot;</span>
            <span style="color: #666666; font-style: italic;">// Añadimos al vector el elemento leído</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_category</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            myParsedCategoryDataSet.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>DataSet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>localName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Ha encontrado la etiqueta de cierre de &quot;id&quot;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_id</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>localName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Ha encontrado la etiqueta de cierre de &quot;name&quot;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_name</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> 
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> characters<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> ch<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, <span style="color: #000066; font-weight: bold;">int</span> start, <span style="color: #000066; font-weight: bold;">int</span> length<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #666666; font-style: italic;">// Si estamos dentro de la etiqueta &quot;id&quot;</span>
       <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       	   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>builder<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       	        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span>start<span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>start<span style="color: #339933;">+</span>length<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// Añadimos al StringBuilder (definido al encontrar el comienzo de la etiqueta &quot;id&quot;)</span>
                    <span style="color: #666666; font-style: italic;">// lo que haya entre las etiquetas de inicio y fin</span>
       	            builder.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ch<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       	        <span style="color: #009900;">&#125;</span>
       	   <span style="color: #009900;">&#125;</span>        	 
           <span style="color: #666666; font-style: italic;">// Lo asignamos al &quot;id&quot; del objeto categoría (DataSet)</span>
       	   DataSet.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span>builder.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
       <span style="color: #009900;">&#125;</span>
&nbsp;
       <span style="color: #666666; font-style: italic;">// Si estamos dentro de la etiqueta &quot;id&quot;</span>
       <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">in_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>builder<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	       <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span>start<span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>start<span style="color: #339933;">+</span>length<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// Añadimos al StringBuilder (definido al encontrar el comienzo de la etiqueta &quot;name&quot;)</span>
                    <span style="color: #666666; font-style: italic;">// lo que haya entre las etiquetas de inicio y fin</span>
    	            builder.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ch<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	        <span style="color: #009900;">&#125;</span>
     	   <span style="color: #009900;">&#125;</span>            	 
           <span style="color: #666666; font-style: italic;">// Lo asignamos al &quot;name&quot; del objeto categoría (DataSet)</span>
           DataSet.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span>builder.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
       <span style="color: #009900;">&#125;</span>   
&nbsp;
   <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>El código está comentado y creo que se explica bastante bien. Me consta que algunos programadores leen el contenido que se encuentra entre las etiquetas de inicio y cierre, cuando llegan a esta última etiqueta en lugar de en el método characters. Yo <strong>me he encontrado con muchos problemas</strong> haciéndolo así ya que se corre el riesgo de <strong>no conseguir leer todos los caracteres que realmente hay</strong>. La propia <a href="http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html#characters(char[], int, int)">documentación del método</a> lo explica:</p>
<p><em>The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, <strong>or they may split it into several chunks</strong>; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.</em></p>
<p>Ya tenemos casi todo lo necesario para <strong>leer el fichero XML de la API</strong>. Solamente nos queda implementar el método que parseará el archivo utilizando nuestra clase.<br />
Esto solo es el extracto del método que lee el XML, encontraréis el código completo de la activity <a href="https://github.com/jonseg/ideas4all_android/blob/master/src/com/jonsegador/ideas4all/Ideas4AllActivity.java">aquí</a>, donde también se hace uso de la combinación de <strong>ProgressDialog</strong> y <strong>Thread</strong> para mostrar un mensaje de &#8220;cargando&#8221; mientras se leen los datos (este tema da para otro post <img src='http://jonsegador.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">...
...
<span style="color: #000000; font-weight: bold;">private</span> Vector<span style="color: #339933;">&lt;</span>ParsedCategoryDataSet<span style="color: #339933;">&gt;</span> categories<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> categories_url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://url_de_la_api/categories.xml&quot;</span><span style="color: #339933;">;</span>
...
...
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> loadCategories<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Url del archivo XML</span>
        <span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>categories_url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        SAXParserFactory spf <span style="color: #339933;">=</span> SAXParserFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        SAXParser sp <span style="color: #339933;">=</span> spf.<span style="color: #006633;">newSAXParser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        XMLReader xr <span style="color: #339933;">=</span> sp.<span style="color: #006633;">getXMLReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Utilizamos nuestro propio parseador (CategoryHandler)</span>
        CategoryHandler myExampleHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CategoryHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        xr.<span style="color: #006633;">setContentHandler</span><span style="color: #009900;">&#40;</span>myExampleHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        InputSource is <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InputSource<span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">openStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Le indicamos la codificación para evitar errores</span>
        is.<span style="color: #006633;">setEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        xr.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Asignamos al vector categories los datos parseados</span>
        categories <span style="color: #339933;">=</span> myExampleHandler.<span style="color: #006633;">getParsedData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// Ha ocurrido algún error</span>
          Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ideas4All&quot;</span>, <span style="color: #0000ff;">&quot;Error&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>        
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Y eso es todo, no olvidéis indicar en el AndroidManifest.xml solicitar permisos de acceso a Internet.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.INTERNET&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/uses-permission<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Podéis encontrar la aplicación completa en github:<br />
<a href="https://github.com/jonseg/ideas4all_android">Código fuente de la aplicación de Ideas4All para Android</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/02/leer-parsear-archivo-xml-en-android-mediante-sax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Seleccionar variable php completa (incluido el &#8216;dolar&#8217;) en Sublime Text 2</title>
		<link>http://jonsegador.com/2012/02/seleccionar-variable-php-completa-incluido-el-dolar-en-sublime-text-2/</link>
		<comments>http://jonsegador.com/2012/02/seleccionar-variable-php-completa-incluido-el-dolar-en-sublime-text-2/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 08:46:41 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sublime text 2]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=764</guid>
		<description><![CDATA[Un detalle de Sublime Text 2 bastante incómodo cuando estás programando en PHP, es que cuando hacemos doble click en una variable para seleccionarla, solamente selecciona el nombre de la variable excluyendo el símbolo dolar ($). La solución es muy sencilla, solo debemos ir a: Preferences / File Settings - Defaults Allí encontraremos la línea: [...]]]></description>
			<content:encoded><![CDATA[<p>Un detalle de <a href="http://jonsegador.com/tag/sublime-text-2/">Sublime Text 2</a> bastante incómodo cuando estás programando en <strong>PHP</strong>, es que cuando hacemos doble click en una variable para seleccionarla, <strong>solamente selecciona el nombre de la variable excluyendo el símbolo dolar ($)</strong>.</p>
<p>La solución es muy sencilla, solo debemos ir a:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Preferences / File Settings - Defaults</pre></div></div>

<p>Allí encontraremos la línea:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;word_separators&quot;: &quot;./\\()\&quot;'-:,.;&lt;&gt;~!@#$%^&amp;*|+=[]{}`~?&quot;,</pre></div></div>

<p>Solamente debemos quitar el símbolo $ de la línea anterior, dejándolo así:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;word_separators&quot;: &quot;./\\()\&quot;'-:,.;&lt;&gt;~!@#%^&amp;*|+=[]{}`~?&quot;,</pre></div></div>

<p>Si ahora cerramos y volvemos a abrir el editor, ya se seleccionará la variable completa.</p>
<p>Truco encontrado aquí:<br />
<a href="http://www.sublimetext.com/forum/viewtopic.php?f=3&#038;t=2704">http://www.sublimetext.com/forum/viewtopic.php?f=3&#038;t=2704</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/02/seleccionar-variable-php-completa-incluido-el-dolar-en-sublime-text-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Paso de datos/variables entre actividades en Android</title>
		<link>http://jonsegador.com/2012/02/paso-de-datos-variables-entre-actividades-android/</link>
		<comments>http://jonsegador.com/2012/02/paso-de-datos-variables-entre-actividades-android/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 20:41:24 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[datos]]></category>
		<category><![CDATA[getExtras]]></category>
		<category><![CDATA[getIntent]]></category>
		<category><![CDATA[intent]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=733</guid>
		<description><![CDATA[Una de las cosas más habituales cuando desarrollamos una aplicación para Android, es poder &#8220;enviar&#8221; datos (variables, objetos, etc.) desde una Activity a otra. En términos generales, una Activity es una &#8220;pantalla&#8221; de nuestra aplicación. Para realizar esta tarea, Android pone a nuestra disposición los llamados &#8220;Intent&#8220;. En general, utilizaremos un Intent cuando queramos movernos [...]]]></description>
			<content:encoded><![CDATA[<p>Una de las cosas más habituales cuando desarrollamos una aplicación para <a href="http://jonsegador.com/category/android/">Android</a>, es poder &#8220;enviar&#8221; datos (variables, objetos, etc.) desde una <strong>Activity</strong> a otra. En términos generales, una Activity es una &#8220;pantalla&#8221; de nuestra aplicación.</p>
<p>Para realizar esta tarea, Android pone a nuestra disposición los llamados &#8220;<strong>Intent</strong>&#8220;. En general, utilizaremos un Intent cuando queramos movernos de una actividad a otra, permitiéndonos a su vez pasar datos desde la Activity en la que estamos hacia la nueva.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Pasaremos de la actividad actual a OtraActivity</span>
Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, OtraActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;variable_integer&quot;</span>, objeto.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;variable_string&quot;</span>, objeto.<span style="color: #006633;">getNombre</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;objeto_float&quot;</span>, objeto.<span style="color: #006633;">getPrecio</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
startActivity<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>- En la segunda línea <strong>creamos un Intent</strong>, al que se le pasa como parámetros la actividad actual (this) y la actividad a la que queremos pasar (en este caso OtraActivity.class).</p>
<p>- En las tres líneas siguientes <strong>definimos tres variables</strong> de tres tipos diferentes. En el primer caso, estaremos creando una variable llamada &#8220;variable_integer&#8221; con el contenido de objeto.getId(). Esta variable se &#8220;envía&#8221; a OtraActivity y <strong>podremos recuperar su contenido</strong> allí para utilizarlo.</p>
<p>- En la última línea, <strong>iniciamos la nueva actividad</strong> pasando como parámetro el Intent que hemos creado.</p>
<p>El código que viene a continuación es el que se ejecutará en la actividad OtraActivity. Se muestran dos formas de recuperar las variables, se puede utilizar la que más convenga en cada ocasión.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Estamos en OtraActivity</span>
<span style="color: #000066; font-weight: bold;">int</span> recuperamos_variable_integer <span style="color: #339933;">=</span> getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getIntExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;variable_integer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> recuperamos_variable_string <span style="color: #339933;">=</span> getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;variable_string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">float</span> recuperamos_variable_float <span style="color: #339933;">=</span> getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getFloatExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;objeto_float&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// O también de esta otra forma</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Estamos en OtraActivity</span>
Bundle datos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getIntent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> recuperamos_variable_integer <span style="color: #339933;">=</span> datos.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;variable_integer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> recuperamos_variable_string <span style="color: #339933;">=</span> datos.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;variable_string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">float</span> recuperamos_variable_float <span style="color: #339933;">=</span> datos.<span style="color: #006633;">getFloat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;objeto_float&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Es importante que sepamos de que tipo son las variables que enviamos en el Intent</strong>, ya que deberemos asignar esos valores al tipo de variable correcto.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/02/paso-de-datos-variables-entre-actividades-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lanzador de Sublime Text 2 en Linux</title>
		<link>http://jonsegador.com/2012/02/lanzador-de-sublime-text-2-en-linux/</link>
		<comments>http://jonsegador.com/2012/02/lanzador-de-sublime-text-2-en-linux/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 11:34:23 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[programación]]></category>
		<category><![CDATA[sublime text]]></category>
		<category><![CDATA[sublime text 2]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=725</guid>
		<description><![CDATA[Sublime Text 2 es un editor de texto que se está haciendo muy popular debido a su facilidad de uso y su entorno minimalista. Sublime Text 2 hace que solo te concentres en el código que estás escribiendo. Si nos descargamos el programa desde su propia web, descubrimos que al descomprimirlo se trata de una [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Sublime Text 2</strong> es un editor de texto que se está haciendo muy popular debido a su <strong>facilidad de uso y su entorno minimalista</strong>. Sublime Text 2 hace que solo te concentres en el código que estás escribiendo.</p>
<p>Si nos descargamos el programa desde su <a href="http://www.sublimetext.com/2">propia web</a>, descubrimos que al descomprimirlo se trata de una carpeta donde se encuentra todo lo necesario para ejecutar el programa. Cada vez que queramos abrirlo, debemos entrar en esta carpeta y ejecutar el archivo de la aplicación (también podemos crear un acceso directo en el escritorio). En esta carpeta no encontraremos ni siquiera el icono de Sublime Text 2.</p>
<p><strong>¿Cómo solucionarlo?</strong></p>
<p>- Primero nos descargamos el <a href="http://jonsegador.com/wp-content/sublime-text-2-icon.png">icono del programa</a> para que el lanzador quede &#8220;bonito&#8221; <img src='http://jonsegador.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>- Creamos un enlace de la aplicación a la carpeta /usr/bin/:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">sudo ln -s /home/jonseg/sublime-text-<span style="color: #cc66cc;">2</span>/sublime_text /usr/bin/sublime_text</pre></div></div>

<p>- Copiamos el icono que nos hemos descargado antes al directorio donde se encuentran los iconos del sistema:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">sudo cp sublime.png /usr/share/icons/</pre></div></div>

<p>- Creamos el archivo sublime.desktop con el siguiente contenido:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Desktop Entry<span style="color: #66cc66;">&#93;</span>
Version=<span style="color: #cc66cc;">2.0</span>
Type=Application
Name=Sublime Text <span style="color: #cc66cc;">2</span>
Comment=Editor Sublime Text <span style="color: #cc66cc;">2</span>
TryExec=sublime_text
Exec=sublime_text <span style="color: #33cc33;">%</span><span style="color: #448888;">F</span>
Icon=/usr/share/icons/sublime.png
MimeType=text/plain;</pre></div></div>

<p>Y lo copiamos al directorio /usr/share/applications/:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">sudo cp sublime.desktop /usr/share/applications/</pre></div></div>

<p>Y listo. Si pulsamos la tecla de Windows de nuestro teclado (en Unity), el resultado debe ser algo parecido a esto:</p>
<p><a href="http://jonsegador.com/wp-content/sublime-text-2-unity.png"><img src="http://jonsegador.com/wp-content/sublime-text-2-unity-300x102.png" alt="" title="sublime-text-2-unity" width="300" height="102" class="aligncenter size-medium wp-image-727" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2012/02/lanzador-de-sublime-text-2-en-linux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

