<?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>Silveira Neto &#187; XML</title>
	<atom:link href="http://silveiraneto.net/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Mon, 19 Jul 2010 21:59:29 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>substitution on a XML file shell script snippet</title>
		<link>http://silveiraneto.net/2010/05/13/substitution-on-a-xml-file-shell-script-snippet/</link>
		<comments>http://silveiraneto.net/2010/05/13/substitution-on-a-xml-file-shell-script-snippet/#comments</comments>
		<pubDate>Thu, 13 May 2010 13:55:38 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3413</guid>
		<description><![CDATA[One more Shell script snippet. Scenario: I have several directories, each one with a file called strings.xml. The problem with those files is that they are like: &#60;string desc=&#34;whatever&#34;&#62;&#34;text&#34;&#60;/string&#62; I&#8217;d like to remove those double quotation marks inside the string element. I&#8217;m already in the directory which contains all others directories with the xml files. [...]]]></description>
			<content:encoded><![CDATA[<p><center><a href="http://www.flickr.com/photos/statelibraryofnsw/3210838977/sizes/m/"><img alt="" src="http://farm4.static.flickr.com/3464/3210838977_5bba45254c.jpg" class="alignnone" width="500" height="389" /></a></center></p>
<p>One more Shell script snippet.</p>
<p><strong>Scenario: </strong>I have several directories, each one with a file called strings.xml. The problem with those files is that they are like:</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">desc</span>=<span style="color: #ff0000;">&quot;whatever&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>&quot;text&quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p>I&#8217;d like to remove those double quotation marks inside the string element.</p>
<p>I&#8217;m already in the directory which contains all others directories with the xml files.</p>
<p><strong>Solution:</strong> A script that for each directory, cat it xml file and substitute the occurrences of >&#8221; for > and &#8220;< for <.</p>
<div class="wp_syntax">
<div class="code">
<pre class="shell" style="font-family:monospace;">for file in `ls`; do cat $file/strings.xml| sed 's/&gt;&quot;/&gt;/g'| sed 's/&quot;&lt;/&lt;/g' &gt; $file/strings.xml; done</pre>
</div>
</div>
<p><strong>Warning:</strong> Shell script and regular expressions are not the correct way to parse XML because there are special cases where you can not handle properly. But in simple cases like this one it can be useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/05/13/substitution-on-a-xml-file-shell-script-snippet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extração de Dados e Fundos de Investimento do Banco do Brasil</title>
		<link>http://silveiraneto.net/2010/01/11/extracao-de-dados-e-fundos-de-investimento-do-banco-do-brasil/</link>
		<comments>http://silveiraneto.net/2010/01/11/extracao-de-dados-e-fundos-de-investimento-do-banco-do-brasil/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 00:02:42 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[ações]]></category>
		<category><![CDATA[aplicações]]></category>
		<category><![CDATA[Banco do Brasil]]></category>
		<category><![CDATA[bolsa de valores]]></category>
		<category><![CDATA[CRON]]></category>
		<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[fundo de investimento]]></category>
		<category><![CDATA[fundos]]></category>
		<category><![CDATA[fundos de investimento]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Mineração de Dados]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[screen scraping]]></category>
		<category><![CDATA[Web Harvest]]></category>
		<category><![CDATA[web scraping]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3112</guid>
		<description><![CDATA[Eu não achei onde coletar os dados diários de rentabilidade dos fundos de investimento do Banco do Brasil em formato bem estruturado. Num mundo ideal as coisas seriam assim, você faria uma requisição numa url como esta: http://bb.com.br/apps/rentabilidade?fundo=Siderurgia&#38;saida=xml E ele cuspiria um XML com as informações da rentabilidade diária desse fundo, isso se eu não [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-3118 aligncenter" title="unicornio" src="http://silveiraneto.net/wp-content/uploads/2010/01/unicornio1.jpg" alt="" width="400" height="440" /></p>
<p>Eu não achei onde coletar os dados diários de rentabilidade dos <a title="Fundos de Investimento do Banco do Brasil" href="http://www21.bb.com.br/portalbb/rentabilidade/index.jsp?tipo=01">fundos de investimento do Banco do Brasil</a> em formato bem estruturado.</p>
<p>Num mundo ideal as coisas seriam assim, você faria uma requisição numa url como esta:</p>
<blockquote><p>http://bb.com.br/apps/rentabilidade?fundo=Siderurgia&amp;saida=xml</p></blockquote>
<p>E ele cuspiria um XML com as informações da rentabilidade diária desse fundo, isso se eu não especificasse através de outro parâmetro qual a data ou intervalo de datas desejado ou outro tipo de dados para saída como YAML ou JSON. Mas por enquanto não temos isso, nem unicórnios, então temos de fazer as coisas do jeito mais difícil, que é puxando os <a title="tabela dos fundos de investimento" href="http://www21.bb.com.br/portalbb/rentabilidade/index.jsp?tipo=01">dados feitos para humanos</a> e escrevendo um programa pra extrair à força os dados que desejamos e quem sabe usar eles para algum uso relacionado a <a href="http://pt.wikipedia.org/wiki/Minera%C3%A7%C3%A3o_de_dados">mineração de dados</a>.</p>
<p style="text-align: center;"><img class="size-full wp-image-3113 aligncenter" title="bb fundos de investimento" src="http://silveiraneto.net/wp-content/uploads/2010/01/bb_fundos_de_investimento.png" alt="" width="494" height="318" /></p>
<p>A primeira abordagem que eu tentei foi a de criar <a href="http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/">um desses pequenos parsers XML que eu já mostrei como fazer antes</a>, mas o código fonte desse documento se mostrou muito incompatível com o XML que o parser estava disposto a trabalhar. A solução alternativa foi tratar o documento linha a linha.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># abrimos o documento referenciado pela url</span>
url = <span style="color: #483d8b;">'http://www21.bb.com.br/portalbb/rentabilidade/index.jsp?tipo=01'</span>
documento = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># fundo de investimento que me interessa</span>
fundo = <span style="color: #483d8b;">'small caps'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># estados</span>
INICIO = 0
ACHOU_FUNDO = <span style="color: #ff4500;">1</span>
FIM = <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># estado inicial</span>
estado = INICIO
&nbsp;
<span style="color: #808080; font-style: italic;"># vamos analisar linha a linha do fluxo do documento</span>
<span style="color: #ff7700;font-weight:bold;">for</span> linha <span style="color: #ff7700;font-weight:bold;">in</span> documento:
	<span style="color: #808080; font-style: italic;"># simplificamos, tudo pra minusculas</span>
	linha = linha.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># no inicio, procura uma linha que tenha o fundo</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> estado == INICIO <span style="color: #ff7700;font-weight:bold;">and</span> linha.<span style="color: black;">find</span><span style="color: black;">&#40;</span>fundo<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">1</span>:
		estado = ACHOU_FUNDO
&nbsp;
	<span style="color: #808080; font-style: italic;"># depois, procuramos o proximo inicio de tabela html.</span>
	<span style="color: #808080; font-style: italic;"># dessa linha, pegamos o que vem depois do primeiro &amp;gt;</span>
	<span style="color: #808080; font-style: italic;"># e entao o que vem antes do primeiro &amp;lt;</span>
	<span style="color: #808080; font-style: italic;"># e trocamos a virgula por ponto.</span>
	<span style="color: #ff7700;font-weight:bold;">elif</span> estado == ACHOU_FUNDO <span style="color: #ff7700;font-weight:bold;">and</span> linha.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&gt;'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span>,<span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>
		estado = FIM</pre>
</div>
</div>
<p>E para usar:</p>
<blockquote><p>$ python rendimento_small_caps.py<br />
0.881</p></blockquote>
<p>Geralmente estamos mais interessados em saber o valor da cota daquele fundo, daí podemos calcular o rendimento total sabendo a cota que compramos a ação inicialmente. Nesse caso o dado está na 11º coluna.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># abrimos o documento referenciado pela url</span>
url = <span style="color: #483d8b;">'http://www21.bb.com.br/portalbb/rentabilidade/index.jsp?tipo=01'</span>
documento = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># fundo de investimento que me interessa</span>
fundo = <span style="color: #483d8b;">'small caps'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># estados</span>
INICIO = 0
ACHOU_FUNDO = <span style="color: #ff4500;">1</span>
FIM = <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># estado inicial</span>
estado = INICIO
coluna = 0
&nbsp;
<span style="color: #808080; font-style: italic;"># vamos analisar linha a linha do fluxo do documento</span>
<span style="color: #ff7700;font-weight:bold;">for</span> linha <span style="color: #ff7700;font-weight:bold;">in</span> documento:
	<span style="color: #808080; font-style: italic;"># simplificamos, tudo pra minusculas</span>
	linha = linha.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># no inicio, procura uma linha que tenha o fundo</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> estado == INICIO <span style="color: #ff7700;font-weight:bold;">and</span> linha.<span style="color: black;">find</span><span style="color: black;">&#40;</span>fundo<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">1</span>:
		estado = ACHOU_FUNDO
&nbsp;
	<span style="color: #808080; font-style: italic;"># para cada coluna, conta a coluna, mas nao faz nada</span>
	<span style="color: #ff7700;font-weight:bold;">elif</span> estado == ACHOU_FUNDO <span style="color: #ff7700;font-weight:bold;">and</span> linha.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;'</span><span style="color: black;">&#41;</span>:
		coluna += <span style="color: #ff4500;">1</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># quando chegar na coluna onze, retira o conteudo entre os sinais &amp;gt; e &amp;lt;</span>
	<span style="color: #808080; font-style: italic;"># e troca virgula por ponto, transforma em float e joga na tela</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> estado==ACHOU_FUNDO <span style="color: #ff7700;font-weight:bold;">and</span> coluna == <span style="color: #ff4500;">11</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>linha.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&gt;'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span>,<span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		estado = FIM</pre>
</div>
</div>
<blockquote><p>$ python cota_small_caps.py<br />
6.156906634</p></blockquote>
<p>Essa é uma abordagem que eu não gosto nem recomendo porque ela é muito frágil e está extremamente acoplada a formatação de dados para humanos. Esta formatação está interessada no saída gráfica que o usuário vai obter e não em facilitar a extração (não humana) desses dados. Isso torna a solução muito frágil:</p>
<ul>
<li>Se mudarem os nomes internos dos elementos, a solução pode falhar.</li>
<li>Se mudarem a formatação da tabela, a solução pode falhar.</li>
<li>Se mudarem a disposição interna dos elementos html, a solução pode falhar.</li>
<li>Se mudarem a url do documento, a solução vai falhar.</li>
<li>Se o documento não puder mais ser tratado linha a linha, a solução vai falhar feio.</li>
</ul>
<p>É provável que quando você estiver lendo isso ela nem funcione mais do jeito que está descrita aqui.</p>
<p>Por outro lado, a solução funciona e nesse caso é o que me interessa. Quando ela quebrar, se ainda for do meu interesse eu posso rapidamente conserta-la e os dados já coletados no passado continuam válidos.</p>
<p>Isso somado  a uma programa como o Cron pode se tornar uma ferramenta realmente poderosa.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/01/11/extracao-de-dados-e-fundos-de-investimento-do-banco-do-brasil/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python Fast XML Parsing</title>
		<link>http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/</link>
		<comments>http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 18:04:50 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[dtd]]></category>
		<category><![CDATA[expat]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sax]]></category>
		<category><![CDATA[schema]]></category>
		<category><![CDATA[urllib]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3081</guid>
		<description><![CDATA[Here is a useful tip on Python XML decoding. I was extending xml.sax.ContentHandler class in a example to decode maps for a Pygame application when my connection went down and I noticed that the program stop working raising a exception regarded a call to urlib (a module for retrieve resources by url). I noticed that [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-3082 aligncenter" title="monty python bunny toy" src="http://silveiraneto.net/wp-content/uploads/2009/12/monty_python_bunny_toy.jpg" alt="" width="175" height="178" /></p>
<p>Here is a useful tip on Python XML decoding.</p>
<p>I was extending <a title="Python Documentation on XML SAX" href="http://docs.python.org/library/xml.sax.html">xml.sax.ContentHandler</a> class in <a title="Tiled TMX Map Loader for Pygame" href="http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/">a example to decode maps for a Pygame application</a> when my connection went down and I noticed that the program stop working raising a exception regarded a call to <a title="Python Documentation on urllib" href="http://docs.python.org/library/urllib.html">urlib</a> (a module for retrieve resources by url). I noticed that the module was getting the remote <a title="Wikipedia on Document Type Definition" href="http://en.wikipedia.org/wiki/Document_Type_Definition">DTD schema</a> to validate the XML.</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE map SYSTEM &quot;http://mapeditor.org/dtd/1.0/map.dtd&quot;&gt;</span></pre>
</div>
</div>
<p>This is not a requirement for my applications and it&#8217;s a huge performance overhead when works (almost 1 second for each map loaded) and when the applications is running in a environment without Internet it just waits for almost a minute and then fail with the remain decoding. A dirty workaround is open the XML file and get rid of the line containing the DTD reference.</p>
<p>But the correct way to programming XML decoding when we are not concerned on validate a XML schema is just the <a href="http://docs.python.org/library/pyexpat.html">xml.parsers.expat</a>. Instead of using a interface you just have to set some callback functions with the behaviors we want. This is a example from the documentation:</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">parsers</span>.<span style="color: black;">expat</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 3 handler functions</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start_element<span style="color: black;">&#40;</span>name, attrs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Start element:'</span>, name, attrs
<span style="color: #ff7700;font-weight:bold;">def</span> end_element<span style="color: black;">&#40;</span>name<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'End element:'</span>, name
<span style="color: #ff7700;font-weight:bold;">def</span> char_data<span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Character data:'</span>, <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;
p = <span style="color: #dc143c;">xml</span>.<span style="color: black;">parsers</span>.<span style="color: black;">expat</span>.<span style="color: black;">ParserCreate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
p.<span style="color: black;">StartElementHandler</span> = start_element
p.<span style="color: black;">EndElementHandler</span> = end_element
p.<span style="color: black;">CharacterDataHandler</span> = char_data
&nbsp;
p.<span style="color: black;">Parse</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;parent id=&quot;top&quot;&gt;&lt;child1 name=&quot;paul&quot;&gt;Text goes here&lt;/child1&gt;
&lt;child2 name=&quot;fred&quot;&gt;More text&lt;/child2&gt;
&lt;/parent&gt;&quot;&quot;&quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>The output:</p>
<pre>
Start element: parent {'id': 'top'}
Start element: child1 {'name': 'paul'}
Character data: 'Text goes here'
End element: child1
Character data: '\n'
Start element: child2 {'name': 'fred'}
Character data: 'More text'
End element: child2
Character data: '\n'
End element: parent
</pre>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaFX, Retrieving non XML/JSON data from clouds</title>
		<link>http://silveiraneto.net/2009/05/31/javafx-retrieving-non-xmljson-data-from-clouds/</link>
		<comments>http://silveiraneto.net/2009/05/31/javafx-retrieving-non-xmljson-data-from-clouds/#comments</comments>
		<pubDate>Sun, 31 May 2009 15:34:42 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[HttpRequest]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PullParser]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Zembly]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2631</guid>
		<description><![CDATA[Usuually on JavaFX we grab data using HttpRequest from external resources on formats like JSON or XML. I showed how to get it on the post Reading Twitter with JavaFX and how to parse it using PullParser on the post Parsing a XML sandwich with JavaFX. Another day I need to grab and interpret some [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-2640 aligncenter" title="weather overcast icon from the Tango Project" src="http://silveiraneto.net/wp-content/uploads/2009/05/tango_weather-overcast.png" alt="tango weather overcast" width="200" height="200" /></p>
<p>Usuually on JavaFX we grab data using <a title="JAVAFX API" href="http://java.sun.com/javafx/1/docs/api/javafx.io.http/javafx.io.http.HttpRequest.html">HttpRequest</a> from external resources on formats like JSON or XML. I showed how to get it on the post <a title="Reading Twitter with JavaFX" href="http://silveiraneto.net/2009/01/04/reding-twitter-with-javafx/">Reading Twitter with JavaFX</a> and how to parse it using <a href="http://java.sun.com/javafx/1/docs/api/javafx.data.pull/javafx.data.pull.PullParser.html">PullParser</a> on the post <a title="Parsing XML sandwich with JavaFX" href="http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/">Parsing a XML sandwich with JavaFX</a>.</p>
<p>Another day I need to grab and interpret some plain results, not in XML nor JSON, while consuming a REST service. In this case we don&#8217;t have a well structure data so the PullParser won&#8217;t help us.</p>
<p><strong>Example 1: Reading Raw Data</strong></p>
<p>In this example we&#8217;ll load a plain text file served in a remote location.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">var planetsRequest <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://silveiraneto.net/downloads/planets&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> <span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var buff <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        var line <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> buff.<span style="color: #006633;">readLine</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: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            println<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
planetsRequest.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>This will produce the output:</p>
<blockquote><p>Mercury<br />
Venus<br />
Earth<br />
Mars<br />
Jupiter<br />
Saturn<br />
Uranus<br />
Neptune</p></blockquote>
<p><strong>Example 2: </strong><strong>Discovering your IP Address</strong></p>
<p>In this example we&#8217;ll examine how to integrate a request of a remote data in a running graphical program.</p>
<p>The best way to know your real IP address is asking for a remote server to look which IP made that request. It&#8217;s like calling for a friend and asking him which number appeared in his mobile. =) This <a href="http://en.wikipedia.org/wiki/Server-side">server side</a> Python script prints the IP address of who requested the page.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Content-type: text/html&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'REMOTE_ADDR'</span><span style="color: black;">&#93;</span></pre>
</div>
</div>
<p>In the client side, with JavaFX, we&#8217;ll load the remote value into a local variable. The <em>ip</em> is assigned with the value &#8220;&#8230;&#8221; and later the ipRequest will replace it with a String with the IP. The <em>bind</em> feature will automatically fix the GUI String text.</p>
<p>For the user he will see the ellipsis for a few seconds and so their IP.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.text.Text</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.io.http.HttpRequest</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span>;
&nbsp;
var ip <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;...&quot;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;What is my IP?&quot;</span> width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">250</span> height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">80</span>
    scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> Text <span style="color: #009900;">&#123;</span>
            x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">30</span>
            content<span style="color: #339933;">:</span> bind <span style="color: #0000ff;">&quot;My IP is {ip}&quot;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var ipRequest <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://silveiraneto.net/scripts/myip.py&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> <span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var buff <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        ip <span style="color: #339933;">=</span> buff.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
ipRequest.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>You can try this <a href="http://silveiraneto.net/downloads/javafx/myip/MyIP.html">JavaFX applet here</a>.</p>
<p><strong>Example 3: </strong><strong>Reading Integer values</strong></p>
<p>Until now we handled just plain Strings. But in some cases you want to get number as non structured data. In this case you need to know previously which type the data is. In the case of a web service this probably will be described in a <a href="http://www.w3.org/TR/wsdl">WSDL file</a>.</p>
<p>Here I&#8217;m writing a very simple service script at <a href="http://zembly.com">Zembly</a>, a great platform for cloud computing. It&#8217;s called aplusb, it justs add the first parameter A to the second B.</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Parameters.<span style="color: #660066;">a</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #009900;">&#40;</span>Parameters.<span style="color: #660066;">b</span><span style="color: #339933;">!=</span> 0<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">return</span> Parameters.<span style="color: #660066;">a</span><span style="color: #339933;">+</span>Parameters.<span style="color: #660066;">b</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>The service is <a href="http://zembly.com/things/1827f696529d4e6f940c36e8e79bea1c#">published at Zembly here</a> where you can see more details on how to invoke it.</p>
<p>A simple way to invoke it on JavaFX and than getting the value as an Integer:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.io.http.HttpRequest</span>;
&nbsp;
var a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span>;
var b <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span>;
var result <span style="color: #339933;">=</span> 0 on replace <span style="color: #009900;">&#123;</span>
    println<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
var zemblyRequest <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://zembly.net/things/1827f696529d4e6f940c36e8e79bea1c;exec?a={a}&amp;amp;b={b}&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> <span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var buff <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        result <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>buff.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
zemblyRequest.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>The output will be:</p>
<blockquote><p>0<br />
300</p></blockquote>
<p>The first 0 is from the first assignment on the var result. The 300 is from the webservice itself.</p>
<p>The same approach can be used to convert the ASCII/Unicode result from the stream to the suitable type on a variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/05/31/javafx-retrieving-non-xmljson-data-from-clouds/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JavaFX, getting resources of inside your JAR</title>
		<link>http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/</link>
		<comments>http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 19:51:54 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[fileinputstream]]></category>
		<category><![CDATA[getclass]]></category>
		<category><![CDATA[JAR]]></category>
		<category><![CDATA[jarfile]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[picture]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[sandwich]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2471</guid>
		<description><![CDATA[Tradução: há uma versão em Português desse artigo. For some classes like javafx.scene.image.Image is easy load an image from a external resource like: ImageView &#123; image: Image &#123; url: &#34;http://example.com/myPicture.png&#34; &#125; &#125; or a resource inside your own Jar file with the __DIR__ constant: ImageView &#123; image: Image &#123; url: &#34;{__DIR__}/myPicture.png&#34; &#125; &#125; But for [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><img src="http://silveiraneto.net/wp-content/uploads/2008/10/br.png" alt="br flag" /><small><strong>Tradução:</strong> há uma <a href="http://silveiraneto.net/2009/04/10/javafx-acessando-recursos-de-dentro-do-jar/">versão em Português</a> desse artigo.</small></p></blockquote>
<p>For some classes like <a href="http://java.sun.com/javafx/1/docs/api/javafx.scene.image/javafx.scene.image.Image.html">javafx.scene.image.Image</a> is easy load an image from a external resource like:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">ImageView <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://example.com/myPicture.png&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>or a resource inside your own Jar file with the __DIR__ constant:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">ImageView <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;{__DIR__}/myPicture.png&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>But for other classes loading a internal resource (inside your own jarfile) is not so direct. For example, in the article <a title="JavaFX XML" href="http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/">Parsing a XML Sandwich with JavaFX</a> I had to place the XML file in a temp directory. A more elegant way would be:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">handlexml</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.ext.swing.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Resource<span style="color: #009900;">&#123;</span>
    function getUrl<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">:</span><span style="color: #003399;">String</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;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    function getStream<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">:</span><span style="color: #003399;">String</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;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var list <span style="color: #339933;">=</span> SwingList <span style="color: #009900;">&#123;</span> width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">600</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#125;</span>
&nbsp;
var myparser <span style="color: #339933;">=</span> PullParser <span style="color: #009900;">&#123;</span>
    documentType<span style="color: #339933;">:</span> PullParser.<span style="color: #006633;">XML</span>;
    onEvent<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var item <span style="color: #339933;">=</span> SwingListItem <span style="color: #009900;">&#123;</span>text<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;event {e}&quot;</span><span style="color: #009900;">&#125;</span>;
        insert item into list.<span style="color: #006633;">items</span>;
    <span style="color: #009900;">&#125;</span>
    input<span style="color: #339933;">:</span> Resource<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>.<span style="color: #006633;">getStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my.xml&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
myparser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Map&quot;</span>
    scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> list
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>With a simple XML file called <em>my.xml</em> inside your package.</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml 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;bread</span> <span style="color: #000066;">sesame</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catchup</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hamburguer</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cheese</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;chedar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maionese</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lettuce</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bread<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p><center><img src="http://silveiraneto.net/wp-content/uploads/2009/04/fileplace.png" alt="fileplace" title="fileplace" width="210" height="107" class="alignnone size-full wp-image-2474" /></center><br />
And we get the same result as before, but all files inside our Jars.<br />
<center><img alt="" src="http://silveiraneto.net/wp-content/uploads/2008/12/javafx_xml_sandwich.png" title="XML Sandwich" class="alignnone" width="625" height="354" /></center></p>
<p>References:</p>
<ul>
<ol>Hildeberto&#8217;s article <a href="http://www.hildeberto.com/2008/12/acessing-resources-inside-of-jar-files.html">Acessing Resources Inside of JAR Files</a></ol>
<ol><a href="http://java.sun.com/javafx/1/tutorials/core/">JavaFX Core Tutorial</a></ol>
<ol>JavaFX forum, <a href="http://forums.sun.com/thread.jspa?threadID=5367468&#038;tstart=2">this</a> and <a href="http://forums.sun.com/thread.jspa?threadID=5340352">this</a> thread.</ol>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parsing a XML Sandwich with JavaFX</title>
		<link>http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/</link>
		<comments>http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 02:39:54 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[jfx]]></category>
		<category><![CDATA[openjfx]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[RIA]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2018</guid>
		<description><![CDATA[Let sandwich.xml be a file at /tmp directory with the content above. &#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62; &#160; &#60;bread sesame=&#34;true&#34;&#62; &#60;catchup/&#62; &#60;hamburguer/&#62; &#60;cheese type=&#34;chedar&#34;/&#62; &#60;maionese/&#62; &#60;lettuce/&#62; &#60;/bread&#62; We can open it using java.io.FileInputStream and so use it on a javafx.data.pull.PullParser. A PullParser is a event oriented parser that works with XML and YAML files. Above a general [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://silveiraneto.net/wp-content/uploads/2008/12/sandwich.jpg" alt="delicious sandwich" title="delicious sandwich" width="400" height="257" class="alignnone size-full wp-image-2022" /></center></p>
<p>Let sandwich.xml be a file at /tmp directory with the content above.</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml 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>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bread</span> <span style="color: #000066;">sesame</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catchup</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hamburguer</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cheese</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;chedar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maionese</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lettuce</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bread<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p>We can open it using java.io.FileInputStream and so use it on a <a href="http://java.sun.com/javafx/1/docs/api/javafx.data.pull/javafx.data.pull.PullParser.html">javafx.data.pull.PullParser</a>. A PullParser is a event oriented parser that works with XML and YAML files. Above a general and simple parser with a GUI that show the list of events during the parse process.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.Event</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.PullParser</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.ext.swing.SwingList</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.ext.swing.SwingListItem</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
&nbsp;
var list <span style="color: #339933;">=</span> SwingList <span style="color: #009900;">&#123;</span> width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">600</span> height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span> <span style="color: #009900;">&#125;</span>
&nbsp;
var myparser <span style="color: #339933;">=</span> PullParser <span style="color: #009900;">&#123;</span>
   documentType<span style="color: #339933;">:</span> PullParser.<span style="color: #006633;">XML</span>;
   onEvent<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      var item <span style="color: #339933;">=</span> SwingListItem <span style="color: #009900;">&#123;</span>
         text<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;event {e}&quot;</span>
      <span style="color: #009900;">&#125;</span>;
      insert item into list.<span style="color: #006633;">items</span>;
   <span style="color: #009900;">&#125;</span>
   input<span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tmp/sandwich.xml&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
myparser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
   title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;XML Sandwich&quot;</span>
   scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span> content<span style="color: #339933;">:</span> list <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p><center><img src="http://silveiraneto.net/wp-content/uploads/2008/12/javafx_xml_sandwich.png" alt="javafx xml sandwich" title="javafx xml sandwich" width="625" height="354" class="alignnone size-full wp-image-2021" /></center></p>
<p>The XML cheese element produce two the outputs.</p>
<blockquote><p>type:1 typeName:START_ELEMENT level:1 qname:cheese text:&#8221; namespaces:{} attributes:{type=chedar}<br />
type:2 typeName:END_ELEMENT level:1 qname:cheese text:&#8221; namespaces:{} attributes:{type=chedar}</p></blockquote>
<p>Notice that white spaces like tab and escape characters like new line also produced events from type TEXT. We are not interested on them. Above a parser that looks only those events of type START_ELEMENT or END_ELEMENT, look into it&#8217;s contents, building a sandwich at runtime based on the XML file.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.Event</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.PullParser</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.xml.QName</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.image.Image</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.image.ImageView</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.layout.VBox</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// my sandwich starts as an empty VBox</span>
var mysandwich <span style="color: #339933;">=</span> VBox <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// give a name and returns a ImageView with a png image like the name</span>
function ingredient<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> ImageView <span style="color: #009900;">&#123;</span>
      image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
         url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;{__DIR__}{name}.png&quot;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// basicaly, look the event and put a ingredient at mysandwich</span>
var myparser <span style="color: #339933;">=</span> PullParser <span style="color: #009900;">&#123;</span>
   documentType<span style="color: #339933;">:</span> PullParser.<span style="color: #006633;">XML</span>;
   onEvent<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// starter xml elements</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">type</span> <span style="color: #339933;">==</span> PullParser.<span style="color: #006633;">START_ELEMENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// bread</span>
         <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bread&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bread_top&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// hamburguer</span>
         <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hamburguer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hamburguer&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// catchup</span>
         <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;catchup&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;catchup&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// maionese</span>
         <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;maionese&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;maionese&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// lettuce</span>
         <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lettuce&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lettuce&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// cheese</span>
         <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cheese&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            var type<span style="color: #339933;">=</span> e.<span style="color: #006633;">getAttributeValue</span><span style="color: #009900;">&#40;</span>QName<span style="color: #009900;">&#123;</span>name<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;type&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>type.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cheedar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                 insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cheedar&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cheese&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
            <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// ending xml elements (just bread)</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">type</span> <span style="color: #339933;">==</span> PullParser.<span style="color: #006633;">END_ELEMENT</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>e.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bread&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            insert ingredient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bread_botton&quot;</span><span style="color: #009900;">&#41;</span> into mysandwich.<span style="color: #006633;">content</span>;
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   input<span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tmp/sandwich.xml&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
myparser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
   title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;XML Sandwich&quot;</span>
   scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span>
      height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span>
      content<span style="color: #339933;">:</span> mysandwich
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Here&#8217;s our sandwich.</p>
<p><center><img src="http://silveiraneto.net/wp-content/uploads/2008/12/sandwich_javafx.png" alt="sandwich javaFX" title="sandwich javaFX" width="335" height="349" class="alignnone size-full wp-image-2024" /></center></p>
<p>Just changing the XML file you got a new sandwich.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;!--</span> <span style="color: #000066; font-weight: bold;">double</span> burger <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>bread sesame<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>maionese<span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>lettuce<span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>hamburguer<span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>cheese type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;chedar&quot;</span><span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>catchup<span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>hamburguer<span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>lettuce<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>bread<span style="color: #339933;">&gt;</span></pre>
</div>
</div>
<p><center><img src="http://silveiraneto.net/wp-content/uploads/2008/12/double_burguer_javafx.png" alt="double burguer " title="double burguer " width="338" height="355" class="alignnone size-full wp-image-2026" /></center></p>
<p>Bon appétit.</p>
<p>For more details on XML and JSON parsing see the <a href="http://java.sun.com/javafx/1/docs/api/javafx.data.pull/javafx.data.pull.PullParser.html">JavaFX API</a>.</p>
<p><small></p>
<ul>
<li><strong>ps:</strong> Sandwich image from <a href="http://commons.wikimedia.org/wiki/File:Lomito1.jpg">commons.wikimedia.org</a></li>
<li><strong>ps2:</strong> We&#8217;re out of sesame.</li>
<li><strong>ps3:</strong> Sources, <a href="http://silveiraneto.net/downloads/xmlsandwich.tar.bz2">xmlsandwich.tar.bz2</a>.</li>
</ul>
<p></small></p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
