<?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>soledad penadés &#187; xml</title>
	<atom:link href="http://soledadpenades.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://soledadpenades.com</link>
	<description>repeat 4[fd 100 rt 90]</description>
	<lastBuildDate>Wed, 25 Apr 2012 21:10:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A hack to parse RSS feeds with php</title>
		<link>http://soledadpenades.com/2012/01/21/a-hack-to-parse-rss-feeds-with-php/</link>
		<comments>http://soledadpenades.com/2012/01/21/a-hack-to-parse-rss-feeds-with-php/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 17:19:37 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[feeds]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3883</guid>
		<description><![CDATA[Just happened to assemble this script hack recently, out of the requirement for a quick&#8217;n'dirty feed parsing feature: $feed_contents = file_get_contents&#40;$feed_url&#41;; $xml = simplexml_load_string&#40;$feed_contents, LIBXML_NOCDATA&#41;; $feed_array = json_decode&#40;json_encode&#40;$xml&#41;&#41;; print_r&#40;$feed_array&#41;; // Surprise!! Now this evidently is not SimplePie or Magpie RSS or whatever feed reader library tickles your fancy*, but assuming the feed will never be [...]]]></description>
			<content:encoded><![CDATA[<p>Just happened to assemble this <del datetime="2012-01-21T16:52:31+00:00">script</del> hack recently, out of the requirement for a quick&#8217;n'dirty feed parsing feature:</p>
<div class="syhi_block"><code><span style="color: #000088;">$feed_contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/simplexml_load_string"><span style="color: #990000;">simplexml_load_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed_contents</span><span style="color: #339933;">,</span> LIBXML_NOCDATA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #000088;">$feed_array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/json_decode"><span style="color: #990000;">json_decode</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Surprise!!</span></code></div>
<p>Now this evidently is <em>not</em> <a href="http://simplepie.org/">SimplePie</a> or <a href="http://magpierss.sourceforge.net/">Magpie RSS</a> or whatever feed reader library tickles your fancy<a href="http://www.phrases.org.uk/bulletin_board/5/messages/888.html"><super>*</super></a>, but assuming the feed will never be malformed, it can save you lots of time! Isn&#8217;t that what php was meant for? :-P<br />
<span id="more-3883"></span><br />
At first it surprised me that it would even work&#8211;but it seems that the JSON module interoperates very nicely with SimpleXML objects. At the end you get an associative array in $feed_array which is a nice representation of the RSS 2 feed. It&#8217;s important to highlight the <strong>LIBXML_NOCDATA</strong> parameter, as it makes sure the CDATA nodes are read as text. Otherwise you would just get empty feed items, etc.</p>
<p>Obviously for this to work you need both the JSON and SimpleXML modules installed and enabled in your server. Since that&#8217;s something that always worries me, I wrote another quick script to test whether the hack would work on the server:</p>
<div class="syhi_block"><code><span style="color: #000088;">$functions</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'simplexml_load_string'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'json_decode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$functions</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' exists? '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></code></div>
<p>It should output something like&#8230;</p>
<div class="syhi_block"><code>simplexml_load_string yes<br />
json_decode yes</code></div>
<p>Happy hacking! :-P</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3883&amp;md5=15f8fcbbdc06d67bd17dfd2a232b5df0" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2012/01/21/a-hack-to-parse-rss-feeds-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2012%2F01%2F21%2Fa-hack-to-parse-rss-feeds-with-php%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=A+hack+to+parse+RSS+feeds+with+php&amp;amp;description=Just+happened+to+assemble+this+script+hack+recently%2C+out+of+the+requirement+for+a+quick%27n%27dirty+feed+parsing+feature%3A%0D%0A%0D%0A%0D%0A%24feed_contents+%3D+file_get_contents%28%24feed_url%29%3B%0D%0A%24xml+%3D+simplexml_load_string%28%24feed_contents%2C+LIBXML_NOCDATA%29%3B+%0D%0A%24feed_array+%3D+json_decode%28json_encode%28%24xml%29%29%3B%0D%0Aprint_r%28%24feed_array%29%3B+%2F%2F+Surprise%21%21%0D%0A%0D%0A%0D%0ANow+this+evidently+is+not+SimplePie+or+Magpie+RSS+or+whatever+feed+reader+library+tickles+your+fancy%2A%2C+but+assuming+the+feed+will+never+be+malformed%2C+it+can+save+you+lots+of+time%21+Isn%27t+that+what+php+was+meant+for%3F+%3A-P%0D%0A%0D%0AAt+first+it+surprised+me+that+it+would+even+work--but+it+seems+that+the+JSON+module+interoperates+very+nicely+with+SimpleXML+objects.+At+the+end+you+get+an+associative+array+in+%24feed_array+which+is+a+nice+representation+of+the+RSS+2+feed.+It%27s+important+to+highlight+the+LIBXML_NOCDATA+parameter%2C+as+it+makes+sure+the+CDATA+nodes+are+read+as+text.+Otherwise+you+would+just+get+empty+feed+items%2C+etc.%0D%0A%0D%0AObviously+for+this+to+work+you+need+both+the+JSON+and+SimpleXML+modules+installed+and+enabled+in+your+server.+Since+that%27s+something+that+always+worries+me%2C+I+wrote+another+quick+script+to+test+whether+the+hack+would+work+on+the+server%3A%0D%0A%0D%0A%0D%0A%24functions+%3D+array%28%27simplexml_load_string%27%2C+%27json_decode%27%29%3B%0D%0Aforeach%28%24functions+as+%24f%29+%7B%0D%0A++echo+%24f+.+%27+exists%3F+%27+.+%28function_exists%28%24f%29+%3F+%27yes%27+%3A+%27no%27%29+.+%27%0A%27%3B%0D%0A%7D%0D%0A%0D%0A%0D%0AIt+should+output+something+like...%0D%0A%0D%0A%0D%0Asimplexml_load_string+yes%0D%0Ajson_decode+yes%0D%0A%0D%0A%0D%0AHappy+hacking%21+%3A-P&amp;amp;tags=feeds%2Chacks%2Cjson%2Cphp%2Crss%2Ctrick%2Cxml%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Reading attribute values of custom components</title>
		<link>http://soledadpenades.com/2010/11/22/reading-attribute-values-of-custom-components/</link>
		<comments>http://soledadpenades.com/2010/11/22/reading-attribute-values-of-custom-components/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 18:18:33 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[attributes]]></category>
		<category><![CDATA[components]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3262</guid>
		<description><![CDATA[Assuming you&#8217;ve defined a custom component which inherits from View and it&#8217;s called MyComponent, create a values/attrs.xml file and add a line for each attribute you want to allow in your component. The file should look more or less like this: &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;resources&#62; &#160; &#160; &#160; &#160; &#60;declare-styleable name=&#34;MyComponent&#34;&#62; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Assuming you&#8217;ve defined a custom component which inherits from <a href="http://developer.android.com/reference/android/view/View.html">View</a> and it&#8217;s called <em>MyComponent</em>, create a <strong>values/attrs.xml</strong> file and add a line for each attribute you want to allow in your component. The file should look more or less like this:</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declare-styleable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyComponent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;android:padding&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- You can add as many attributes as required --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/declare-styleable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></code></div>
<p>If you also want to add custom attributes, you need to define a namespace in the layout file that uses components with custom attributes. Something like this:</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span> <span style="color: #000066;">xmlns:myNamespace</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/com.mycompany.projectname&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- stuff --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></code></div>
<p>And in <strong>values/attrs.xml</strong> you&#8217;ll use <em>myNamespace:</em> instead of <em>android:</em> when listing the supported attributes:</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declare-styleable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyComponent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myNamespace:esotericAttribute&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/declare-styleable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></code></div>
<p>Of course both can be mixed!</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declare-styleable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyComponent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;android:padding&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myNamespace:esotericAttribute&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/declare-styleable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></code></div>
<p>Then in your layout file declare your components and required attributes as usual:</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span> <span style="color: #000066;">xmlns:myNamespace</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/com.mycompany.projectname&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;com.mycompany.projectname.MyComponent</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">android:padding</span>=<span style="color: #ff0000;">&quot;10dip&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">myNamespace:esotericAttribute</span>=<span style="color: #ff0000;">&quot;Always&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></code></div>
<p>We&#8217;ll read their values in the constructor which accepts <a href="http://developer.android.com/reference/android/content/Context.html">Context</a> and <a href="http://developer.android.com/reference/android/util/AttributeSet.html">AttributeSet</a> as parameters:</p>
<div class="syhi_block"><code><span style="color: #000000; font-weight: bold;">public</span> MyComponent<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Context</span></a> context, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aattributeset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">AttributeSet</span></a> attrs<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context, attrs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> padding<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> esotericAttribute<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; TypedArray theAttrs <span style="color: #339933;">=</span> context.<span style="color: #006633;">obtainStyledAttributes</span><span style="color: #009900;">&#40;</span>attrs, R.<span style="color: #006633;">styleable</span>.<span style="color: #006633;">MyButton</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Gets the value &quot;as is&quot;, with no further conversions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; esotericAttribute <span style="color: #339933;">=</span> theAttrs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">styleable</span>.<span style="color: #006633;">MyComponent_myNamespace_esotericAttribute</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Scaled &quot;device independent&quot; value, 0 is the default if attribute is not specified</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; padding <span style="color: #339933;">=</span> theAttrs.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">styleable</span>.<span style="color: #006633;">MyComponent_android_padding</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// call this when you're done with the attributes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; theAttrs.<span style="color: #006633;">recycle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// now you would do stuff with the read attribute values&nbsp; &nbsp; &nbsp; &nbsp; ...</span><br />
<span style="color: #009900;">&#125;</span></code></div>
<p>A note on getDimensionPixelSize: yes, you could be &#8220;brave&#8221; and use getString instead of getDimensionPixel Size, but you&#8217;d also have to parse the values and make sure both the values and the units are right, and take care of scaling the readings so that they would be appropriate for the display size in which the application is running. </p>
<p>But it&#8217;s evident that it&#8217;s way more comfortable to let getDimensionPixelSize do all that work for you!</p>
<p>For example, in an HTC Evo 4G, which is &#8220;high density&#8221;, getDimensionPixelSize for a padding of 10dip returns 15 pixels = 1.5 * 10, where 1.5 is the display density.</p>
<p>References and more information:</p>
<ul>
<li><a href="http://developer.android.com/guide/topics/ui/custom-components.html">Custom components</a></li>
<li><a href="http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml/2695649">Declaring a custom Android UI element using XML</a></li>
</ul>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3262&amp;md5=756c9b3647f340930710f02aaa214e94" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2010/11/22/reading-attribute-values-of-custom-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2010%2F11%2F22%2Freading-attribute-values-of-custom-components%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=Reading+attribute+values+of+custom+components&amp;amp;description=Assuming+you%27ve+defined+a+custom+component+which+inherits+from+View+and+it%27s+called+MyComponent%2C+create+a+values%2Fattrs.xml+file+and+add+a+line+for+each+attribute+you+want+to+allow+in+your+component.+The+file+should+look+more+or+less+like+this%3A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%0D%0A%0D%0A%0D%0A%0D%0AIf+you+also+want+to+add+custom+attributes%2C+you+need+to+define+a+namespace+in+the+layout+file+that+uses+components+with+custom+attributes.+Something+like+this%3A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0AAnd+in+values%2Fattrs.xml+you%27ll+use+myNamespace%3A+instead+of+android%3A+when+listing+the+supported+attributes%3A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%09%0D%0A%09%09%0D%0A%09%0D%0A%0D%0A%0D%0A%0D%0AOf+course+both+can+be+mixed%21%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%0D%0A%0D%0A%0D%0A%0D%0AThen+in+your+layout+file+declare+your+components+and+required+attributes+as+usual%3A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%09%0D%0A%0D%0A%0D%0A%0D%0AWe%27ll+read+their+values+in+the+constructor+which+accepts+Context+and+AttributeSet+as+parameters%3A%0D%0A%0D%0A%0D%0Apublic+MyComponent%28Context+context%2C+AttributeSet+attrs%29%0D%0A%7B%0D%0A%09super%28context%2C+attrs%29%3B%0D%0A%09int+padding%3B%0D%0A%09String+esotericAttribute%3B%0D%0A%0D%0A%09TypedArray+theAttrs+%3D+context.obtainStyledAttributes%28attrs%2C+R.styleable.MyButton%29%3B%0D%0A%0D%0A%09%2F%2F+Gets+the+value+%22as+is%22%2C+with+no+further+conversions%0D%0A%09esotericAttribute+%3D+theAttrs.getString%28R.styleable.MyComponent_myNamespace_esotericAttribute%29%3B%0D%0A%0D%0A%09%2F%2F+Scaled+%22device+independent%22+value%2C+0+is+the+default+if+attribute+is+not+specified%0D%0A%09padding+%3D+theAttrs.getDimensionPixelSize%28R.styleable.MyComponent_android_padding%2C+0%29%3B%0D%0A%0D%0A%09%2F%2F+call+this+when+you%27re+done+with+the+attributes%0D%0A%09theAttrs.recycle%28%29%3B%0D%0A%0D%0A%09%2F%2F+now+you+would+do+stuff+with+the+read+attribute+values%09...%0D%0A%7D%0D%0A%0D%0A%0D%0AA+note+on+getDimensionPixelSize%3A+yes%2C+you+could+be+%22brave%22+and+use+getString+instead+of+getDimensionPixel+Size%2C+but+you%27d+also+have+to+parse+the+values+and+make+sure+both+the+values+and+the+units+are+right%2C+and+take+care+of+scaling+the+readings+so+that+they+would+be+appropriate+for+the+display+size+in+which+the+application+is+running.+%0D%0A%0D%0ABut+it%27s+evident+that+it%27s+way+more+comfortable+to+let+getDimensionPixelSize+do+all+that+work+for+you%21%0D%0A%0D%0AFor+example%2C+in+an+HTC+Evo+4G%2C+which+is+%22high+density%22%2C+getDimensionPixelSize+for+a+padding+of+10dip+returns+15+pixels+%3D+1.5+%2A+10%2C+where+1.5+is+the+display+density.%0D%0A%0D%0AReferences+and+more+information%3A%0D%0A%0D%0A%09Custom+components%0D%0A%09Declaring+a+custom+Android+UI+element+using+XML%0D%0A%0D%0A%0D%0A&amp;amp;tags=android%2Cattributes%2Ccomponents%2Ccustom%2Cview%2Cxml%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Tiled backgrounds in Android</title>
		<link>http://soledadpenades.com/2010/11/19/tiled-backgrounds-in-android/</link>
		<comments>http://soledadpenades.com/2010/11/19/tiled-backgrounds-in-android/#comments</comments>
		<pubDate>Fri, 19 Nov 2010 21:58:53 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[bitmapdrawable]]></category>
		<category><![CDATA[tiling]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3256</guid>
		<description><![CDATA[If you just point your UI element to an image resource, like this: android:background=&#34;@drawable/myBackgroundImage&#34; you won&#8217;t get a tiled background. Instead, you&#8217;ll get the image stretched to the element size! Really horrible, if that&#8217;s not what you intended to do. The trick is to create another XML file in the drawable directory. This file contains [...]]]></description>
			<content:encoded><![CDATA[<p>If you just point your UI element to an image resource, like this:</p>
<div class="syhi_block"><code>android:background=&quot;@drawable/myBackgroundImage&quot;</code></div>
<p>you won&#8217;t get a tiled background. Instead, you&#8217;ll get the image stretched to the element size! Really horrible, if that&#8217;s not what you intended to do.</p>
<p>The trick is to create <em>another</em> XML file in the <strong>drawable</strong> directory. </p>
<p>This file contains configuration settings which describe how an image should behave when used as a tiling background:</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bitmap</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:src</span>=<span style="color: #ff0000;">&quot;@drawable/myBackgroundImage&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:tileMode</span>=<span style="color: #ff0000;">&quot;repeat&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></code></div>
<p>Let&#8217;s assume you saved it as <strong>drawable/tiledBackground.xml</strong>. Now, in your UI element you just simply have to refer to this newly created XML file instead of referring to the background image:</p>
<div class="syhi_block"><code>android:background=&quot;@drawable/tiledBackground&quot;</code></div>
<p>It&#8217;s as if the XML file creates a virtual image out of the original image, except the new image can be tiled. In fact, what we are creating with this XML is a <a href="http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html">BitmapDrawable</a>.</p>
<p>In the documentation for that class we can see that <em>android:tileMode</em> accepts the following values: </p>
<ul>
<li>repeat (the expected, intuitive tiling),</li>
<li>mirror (which makes a non seamless pattern <em>seamless</em>!),</li>
<li>and clamp (which repeats the edge colour).</li>
</ul>
<p>It first occurred to me that this approach of having to create an XML file for defining tiles was a little over the top, but then I thought that maybe it&#8217;s very useful if you want to reuse the same settings across a large collection of items. For example, if you want to define your own collection of UI widgets. So I guess that in that context it makes sense&#8230;</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3256&amp;md5=2c74e0f82cf04d5488487bdd8bcecc81" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2010/11/19/tiled-backgrounds-in-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2010%2F11%2F19%2Ftiled-backgrounds-in-android%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=Tiled+backgrounds+in+Android&amp;amp;description=If+you+just+point+your+UI+element+to+an+image+resource%2C+like+this%3A%0D%0A%0D%0A%0D%0Aandroid%3Abackground%3D%22%40drawable%2FmyBackgroundImage%22%0D%0A%0D%0A%0D%0Ayou+won%27t+get+a+tiled+background.+Instead%2C+you%27ll+get+the+image+stretched+to+the+element+size%21+Really+horrible%2C+if+that%27s+not+what+you+intended+to+do.%0D%0A%0D%0AThe+trick+is+to+create+another+XML+file+in+the+drawable+directory.+%0D%0A%0D%0AThis+file+contains+configuration+settings+which+describe+how+an+image+should+behave+when+used+as+a+tiling+background%3A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0ALet%27s+assume+you+saved+it+as+drawable%2FtiledBackground.xml.+Now%2C+in+your+UI+element+you+just+simply+have+to+refer+to+this+newly+created+XML+file+instead+of+referring+to+the+background+image%3A%0D%0A%0D%0A%0D%0Aandroid%3Abackground%3D%22%40drawable%2FtiledBackground%22%0D%0A%0D%0A%0D%0AIt%27s+as+if+the+XML+file+creates+a+virtual+image+out+of+the+original+image%2C+except+the+new+image+can+be+tiled.+In+fact%2C+what+we+are+creating+with+this+XML+is+a+BitmapDrawable.%0D%0A%0D%0AIn+the+documentation+for+that+class+we+can+see+that+android%3AtileMode+accepts+the+following+values%3A+%0D%0A%0D%0A%09repeat+%28the+expected%2C+intuitive+tiling%29%2C%0D%0A%09mirror+%28which+makes+a+non+seamless+pattern+seamless%21%29%2C%0D%0A%09and+clamp+%28which+repeats+the+edge+colour%29.%0D%0A%0D%0A%0D%0AIt+first+occurred+to+me+that+this+approach+of+having+to+create+an+XML+file+for+defining+tiles+was+a+little+over+the+top%2C+but+then+I+thought+that+maybe+it%27s+very+useful+if+you+want+to+reuse+the+same+settings+across+a+large+collection+of+items.+For+example%2C+if+you+want+to+define+your+own+collection+of+UI+widgets.+So+I+guess+that+in+that+context+it+makes+sense...&amp;amp;tags=android%2Cbackground%2Cbitmapdrawable%2Ctiling%2Ctrick%2Cxml%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Converting &#8216;My Tracks&#8217; KML files for DDMS</title>
		<link>http://soledadpenades.com/2010/10/25/converting-my-tracks-kml-files-for-ddms/</link>
		<comments>http://soledadpenades.com/2010/10/25/converting-my-tracks-kml-files-for-ddms/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 22:53:24 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[ddms]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[kml]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3069</guid>
		<description><![CDATA[KML files store geographical information about paths. They can be generated with applications such as Android&#8217;s My Tracks application, and in theory we should be able to load them into the emulator, using the DDMS Eclipse Plug-in, to debug and test applications which depend on the user&#8217;s location without having to be actually out in [...]]]></description>
			<content:encoded><![CDATA[<p>KML files store geographical information about paths. They can be generated with applications such as Android&#8217;s <a href="http://www.cyrket.com/p/android/com.google.android.maps.mytracks/">My Tracks</a> application, and <em>in theory</em> we should be able to load them into the emulator, using the DDMS Eclipse Plug-in, to debug and test applications which depend on the user&#8217;s location without having to be actually out in the street&#8230;</p>
<p>&#8230; but I sadly discovered that the files generated with <em>My Tracks</em> do not work :-(</p>
<p>Looking for answers, I found an online application that supposedly converts the files into something that DDMS would accept, but apart from the fact that it didn&#8217;t work at all, it also required me to log in with my Google Account in order to use it (why?)</p>
<p><strong>So I decided to write my own converter</strong>, in Javascript, and put it online, so that you don&#8217;t have to lose time as I did. <a href="http://lab.soledadpenades.com/android/kml/">It&#8217;s here</a>. It&#8217;s free, it&#8217;s fast, it won&#8217;t pester you asking you to log in, and most specially, <strong>it works</strong>.</p>
<h3>For the curious in you</h3>
<p>Since reading the KML specification is about the most tedious thing I&#8217;ve ever done on a Friday afternoon, I ended up writing a working KML file by the classical method of deduction, trial and error. After all, they are simply XML files.</p>
<p>Also, it seems the DDMS plug-in only expects a series of <em>Placemarks</em>, and will list them once loaded, one by line, with the <em>Longitude</em>, <em>Latitude</em>, <em>Elevation</em> and <em>Description</em> columns&#8211;you can then select distinct locations in DDMS, and they are sent to the emulator via geo commands (I presume that this is a nice way of abstracting the telnet connection underneath). Having them listed is a way to debug and make sure you&#8217;re getting the proper data into DDMS :-D</p>
<p>The structure of a working KML file is as follows:</p>
<div class="syhi_block"><code><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><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;kml</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://earth.google.com/kml/2.x&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-122.08220,37.42228,0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-122.083,37.4222851,0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- More placemarks ... --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/kml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></code></div>
<p>KML files generated by <em>My Tracks</em> have an insane amount of extraneous data and I won&#8217;t even bother to post them! Just trust me :-)</p>
<p>My script takes a very naive approach: when you paste the malformed KML into the textarea and press <em>Convert</em>, it will create and load an XML document with the data from the textarea, and then look for <strong>&lt;coordinates&gt;</strong> nodes using the good old <strong>getElementsByTagName</strong> node method. Therefore, it&#8217;s possible to get duplicated values (for example, the start and end markers are reported as separate placemarkers, although their coordinates are already included in the main path).</p>
<p>There&#8217;s something extra to consider: all the values <strong>must</strong> specify an elevation value. Otherwise, DDMS <strong>will not</strong> load the KML file. So when parsing the coordinates I&#8217;m splitting their values and making sure that they have three values; otherwise I add an extra default &#8217;0&#8242; for elevation.</p>
<p>One last thing. It&#8217;s been quite fun to do this with Javascript, but I&#8217;m quite surprised that it took me more time to find out how to create an XML document from a String, than actually writing the script! Finding about this line:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> xml <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> DOMParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parseFromString</span><span style="color: #009900;">&#40;</span>xml_text<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;application/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<p>has taken me ages and dozens of tabs!</p>
<p>I would have expected that handling XML with Javascript would have been a solved problem by now, but it seems otherwise. There was a lot of people talking about AJAX and XMLHttpRequest (no wonder! XML was the X in AJA<strong>X</strong>), others solving everything with a <q>use JSON!</q> and some other people suggesting a certain XML() constructor which never worked, IE proprietary methods or even E4X which isn&#8217;t implemented in Chrome/Webkit. And it was so simple in fact! But so counter-intuitive&#8230;</p>
<p>Anyway, there it is. Don&#8217;t forget about it, kids: <strong>new DOMParser()</strong> is the answer :P</p>
<p>PS Oh I also used the new HTML5 <em>placeholder</em> attribute in the textareas. It doesn&#8217;t work on Firefox 3.6 but it does on Chrome, so it&#8217;s cool anyway.</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3069&amp;md5=f506998686f282a68e68bb3a08a32d3b" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2010/10/25/converting-my-tracks-kml-files-for-ddms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2010%2F10%2F25%2Fconverting-my-tracks-kml-files-for-ddms%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=Converting+%26%238216%3BMy+Tracks%26%238217%3B+KML+files+for+DDMS&amp;amp;description=KML+files+store+geographical+information+about+paths.+They+can+be+generated+with+applications+such+as+Android%27s+My+Tracks+application%2C+and+in+theory+we+should+be+able+to+load+them+into+the+emulator%2C+using+the+DDMS+Eclipse+Plug-in%2C+to+debug+and+test+applications+which+depend+on+the+user%27s+location+without+having+to+be+actually+out+in+the+street...%0D%0A%0D%0A...+but+I+sadly+discovered+that+the+files+generated+with+My+Tracks+do+not+work+%3A-%28%0D%0A%0D%0ALooking+for+answers%2C+I+found+an+online+application+that+supposedly+converts+the+files+into+something+that+DDMS+would+accept%2C+but+apart+from+the+fact+that+it+didn%27t+work+at+all%2C+it+also+required+me+to+log+in+with+my+Google+Account+in+order+to+use+it+%28why%3F%29%0D%0A%0D%0ASo+I+decided+to+write+my+own+converter%2C+in+Javascript%2C+and+put+it+online%2C+so+that+you+don%27t+have+to+lose+time+as+I+did.+It%27s+here.+It%27s+free%2C+it%27s+fast%2C+it+won%27t+pester+you+asking+you+to+log+in%2C+and+most+specially%2C+it+works.%0D%0A%0D%0AFor+the+curious+in+you%0D%0A%0D%0ASince+reading+the+KML+specification+is+about+the+most+tedious+thing+I%27ve+ever+done+on+a+Friday+afternoon%2C+I+ended+up+writing+a+working+KML+file+by+the+classical+method+of+deduction%2C+trial+and+error.+After+all%2C+they+are+simply+XML+files.%0D%0A%0D%0AAlso%2C+it+seems+the+DDMS+plug-in+only+expects+a+series+of+Placemarks%2C+and+will+list+them+once+loaded%2C+one+by+line%2C+with+the+Longitude%2C+Latitude%2C+Elevation+and+Description+columns--you+can+then+select+distinct+locations+in+DDMS%2C+and+they+are+sent+to+the+emulator+via+geo+commands+%28I+presume+that+this+is+a+nice+way+of+abstracting+the+telnet+connection+underneath%29.+Having+them+listed+is+a+way+to+debug+and+make+sure+you%27re+getting+the+proper+data+into+DDMS+%3A-D%0D%0A%0D%0AThe+structure+of+a+working+KML+file+is+as+follows%3A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%09%0D%0A%09%091%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%09-122.08220%2C37.42228%2C0%0D%0A%09%09%0D%0A%09%0D%0A%09%0D%0A%09%092%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%09-122.083%2C37.4222851%2C0%0D%0A%09%09%0D%0A%09%0D%0A%09%0D%0A%0D%0A%0D%0A%0D%0AKML+files+generated+by+My+Tracks+have+an+insane+amount+of+extraneous+data+and+I+won%27t+even+bother+to+post+them%21+Just+trust+me+%3A-%29%0D%0A%0D%0AMy+script+takes+a+very+naive+approach%3A+when+you+paste+the+malformed+KML+into+the+textarea+and+press+Convert%2C+it+will+create+and+load+an+XML+document+with+the+data+from+the+textarea%2C+and+then+look+for+%26lt%3Bcoordinates%26gt%3B+nodes+using+the+good+old+getElementsByTagName+node+method.+Therefore%2C+it%27s+possible+to+get+duplicated+values+%28for+example%2C+the+start+and+end+markers+are+reported+as+separate+placemarkers%2C+although+their+coordinates+are+already+included+in+the+main+path%29.%0D%0A%0D%0AThere%27s+something+extra+to+consider%3A+all+the+values+must+specify+an+elevation+value.+Otherwise%2C+DDMS+will+not+load+the+KML+file.+So+when+parsing+the+coordinates+I%27m+splitting+their+values+and+making+sure+that+they+have+three+values%3B+otherwise+I+add+an+extra+default+%270%27+for+elevation.%0D%0A%0D%0AOne+last+thing.+It%27s+been+quite+fun+to+do+this+with+Javascript%2C+but+I%27m+quite+surprised+that+it+took+me+more+time+to+find+out+how+to+create+an+XML+document+from+a+String%2C+than+actually+writing+the+script%21+Finding+about+this+line%3A%0D%0A%0D%0A%0D%0Avar+xml+%3D+%28new+DOMParser%28%29%29.parseFromString%28xml_text%2C+%22application%2Fxml%22%29%3B%0D%0A%0D%0A%0D%0Ahas+taken+me+ages+and+dozens+of+tabs%21%0D%0A%0D%0AI+would+have+expected+that+handling+XML+with+Javascript+would+have+been+a+solved+problem+by+now%2C+but+it+seems+otherwise.+There+was+a+lot+of+people+talking+about+AJAX+and+XMLHttpRequest+%28no+wonder%21+XML+was+the+X+in+AJAX%29%2C+others+solving+everything+with+a+use+JSON%21+and+some+other+people+suggesting+a+certain+XML%28%29+constructor+which+never+worked%2C+IE+proprietary+methods+or+even+E4X+which+isn%27t+implemented+in+Chrome%2FWebkit.+And+it+was+so+simple+in+fact%21+But+so+counter-intuitive...%0D%0A%0D%0AAnyway%2C+there+it+is.+Don%27t+forget+about+it%2C+kids%3A+new+DOMParser%28%29+is+the+answer+%3AP%0D%0A%0D%0APS+Oh+I+also+used+the+new+HTML5+placeholder+attribute+in+the+textareas.+It+doesn%27t+work+on+Firefox+3.6+but+it+does+on+Chrome%2C+so+it%27s+cool+anyway.&amp;amp;tags=android%2Cddms%2Cgeolocation%2Cgps%2Cjavascript%2Ckml%2Ctools%2Cxml%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Breakpoint demolog, day 19: loading Song.xml == DONE</title>
		<link>http://soledadpenades.com/2010/02/21/breakpoint-demolog-day-19-loading-song-xml-done/</link>
		<comments>http://soledadpenades.com/2010/02/21/breakpoint-demolog-day-19-loading-song-xml-done/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 01:19:16 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[breakpoint]]></category>
		<category><![CDATA[demoscene]]></category>
		<category><![CDATA[renoise]]></category>
		<category><![CDATA[trackers]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=2407</guid>
		<description><![CDATA[I didn&#8217;t really feel like doing too much today but I managed to convince myself to finish the loading of relevant data from the song&#8217;s file. I now only have to instantiate patterns, pattern entries and synth voices and send them that relevant data when loading. And adapt my old tracker-style playing routine to work [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t really feel like doing too much today but I managed to convince myself to finish the loading of relevant data from the song&#8217;s file.</p>
<p>I now <em>only</em> have to instantiate patterns, pattern entries and synth voices and send them that relevant data when loading. And adapt my old tracker-style playing routine to work with Renoise 2&#8242;s new philosophy (they got rid of the &#8220;speed&#8221; global song parameter which we were carrying on from the old-tracker era, but I was using that parameter in my player).</p>
<p>There&#8217;s not much to show yet, but anyway here&#8217;s an extract of the information the test program is outputting while loading a song:</p>
<div class="syhi_block"><code>Pattern 0, 64 lines<br />
Track 0<br />
Line 0&nbsp; only note OFF<br />
Line 1&nbsp; Line 2&nbsp; instrument 03 note A-4<br />
Line 3&nbsp; Line 4&nbsp; only note OFF<br />
Line 5&nbsp; Line 6&nbsp; instrument 03 note A-4<br />
Line 7&nbsp; Line 8&nbsp; only note OFF<br />
Line 9&nbsp; Line 10 instrument 03 note A-4</code></div>
<p>So that you can see that I&#8217;m not slacking ;)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=2407&amp;md5=0203fea1240a6aaafb894627c1f43a97" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2010/02/21/breakpoint-demolog-day-19-loading-song-xml-done/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2010%2F02%2F21%2Fbreakpoint-demolog-day-19-loading-song-xml-done%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=Breakpoint+demolog%2C+day+19%3A+loading+Song.xml+%3D%3D+DONE&amp;amp;description=I+didn%27t+really+feel+like+doing+too+much+today+but+I+managed+to+convince+myself+to+finish+the+loading+of+relevant+data+from+the+song%27s+file.%0D%0A%0D%0AI+now+only+have+to+instantiate+patterns%2C+pattern+entries+and+synth+voices+and+send+them+that+relevant+data+when+loading.+And+adapt+my+old+tracker-style+playing+routine+to+work+with+Renoise+2%27s+new+philosophy+%28they+got+rid+of+the+%22speed%22+global+song+parameter+which+we+were+carrying+on+from+the+old-tracker+era%2C+but+I+was+using+that+parameter+in+my+player%29.%0D%0A%0D%0AThere%27s+not+much+to+show+yet%2C+but+anyway+here%27s+an+extract+of+the+information+the+test+program+is+outputting+while+loading+a+song%3A%0D%0A%0D%0A%0D%0APattern+0%2C+64+lines%0D%0ATrack+0%0D%0ALine+0%09only+note+OFF%0D%0ALine+1%09Line+2%09instrument+03+note+A-4%0D%0ALine+3%09Line+4%09only+note+OFF%0D%0ALine+5%09Line+6%09instrument+03+note+A-4%0D%0ALine+7%09Line+8%09only+note+OFF%0D%0ALine+9%09Line+10%09instrument+03+note+A-4%0D%0A%0D%0A%0D%0ASo+that+you+can+see+that+I%27m+not+slacking+%3B%29&amp;amp;tags=breakpoint%2Cdemoscene%2Crenoise%2Ctrackers%2Cxml%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>

