<?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; yaml</title>
	<atom:link href="http://silveiraneto.net/tag/yaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<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>4</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>

