come with me, on the way I'll explain.
Posts tagged XML
Extração de Dados e Fundos de Investimento do Banco do Brasil
Jan 11th

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&saida=xml
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 dados feitos para humanos e escrevendo um programa pra extrair à força os dados que desejamos e quem sabe usar eles para algum uso relacionado a mineração de dados.

A primeira abordagem que eu tentei foi a de criar um desses pequenos parsers XML que eu já mostrei como fazer antes, 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.
import urllib # abrimos o documento referenciado pela url url = 'http://www21.bb.com.br/portalbb/rentabilidade/index.jsp?tipo=01' documento = urllib.urlopen(url) # fundo de investimento que me interessa fundo = 'small caps' # estados INICIO = 0 ACHOU_FUNDO = 1 FIM = 2 # estado inicial estado = INICIO # vamos analisar linha a linha do fluxo do documento for linha in documento: # simplificamos, tudo pra minusculas linha = linha.lower() # no inicio, procura uma linha que tenha o fundo if estado == INICIO and linha.find(fundo) != -1: estado = ACHOU_FUNDO # depois, procuramos o proximo inicio de tabela html. # dessa linha, pegamos o que vem depois do primeiro > # e entao o que vem antes do primeiro < # e trocamos a virgula por ponto. elif estado == ACHOU_FUNDO and linha.find(' ')[1].split('<')[0].replace(',','.') estado = FIM
E para usar:
$ python rendimento_small_caps.py
0.881
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.
import urllib # abrimos o documento referenciado pela url url = 'http://www21.bb.com.br/portalbb/rentabilidade/index.jsp?tipo=01' documento = urllib.urlopen(url) # fundo de investimento que me interessa fundo = 'small caps' # estados INICIO = 0 ACHOU_FUNDO = 1 FIM = 2 # estado inicial estado = INICIO coluna = 0 # vamos analisar linha a linha do fluxo do documento for linha in documento: # simplificamos, tudo pra minusculas linha = linha.lower() # no inicio, procura uma linha que tenha o fundo if estado == INICIO and linha.find(fundo) != -1: estado = ACHOU_FUNDO # para cada coluna, conta a coluna, mas nao faz nada elif estado == ACHOU_FUNDO and linha.find(' <= 11: coluna += 1 # quando chegar na coluna onze, retira o conteudo entre os sinais > e < # e troca virgula por ponto, transforma em float e joga na tela if estado==ACHOU_FUNDO and coluna == 11: print float(linha.split('>')[1].split('<')[0].replace(',','.')) estado = FIM
$ python cota_small_caps.py
6.156906634
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:
- Se mudarem os nomes internos dos elementos, a solução pode falhar.
- Se mudarem a formatação da tabela, a solução pode falhar.
- Se mudarem a disposição interna dos elementos html, a solução pode falhar.
- Se mudarem a url do documento, a solução vai falhar.
- Se o documento não puder mais ser tratado linha a linha, a solução vai falhar feio.
É provável que quando você estiver lendo isso ela nem funcione mais do jeito que está descrita aqui.
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.
Isso somado a uma programa como o Cron pode se tornar uma ferramenta realmente poderosa.
Python Fast XML Parsing
Dec 25th

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 the module was getting the remote DTD schema to validate the XML.
<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
This is not a requirement for my applications and it’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.
But the correct way to programming XML decoding when we are not concerned on validate a XML schema is just the xml.parsers.expat. 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:
import xml.parsers.expat # 3 handler functions def start_element(name, attrs): print 'Start element:', name, attrs def end_element(name): print 'End element:', name def char_data(data): print 'Character data:', repr(data) p = xml.parsers.expat.ParserCreate() p.StartElementHandler = start_element p.EndElementHandler = end_element p.CharacterDataHandler = char_data p.Parse("""<?xml version="1.0"?> <parent id="top"><child1 name="paul">Text goes here</child1> <child2 name="fred">More text</child2> </parent>""", 1)
The output:
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
JavaFX, Retrieving non XML/JSON data from clouds
May 31st

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 plain results, not in XML nor JSON, while consuming a REST service. In this case we don’t have a well structure data so the PullParser won’t help us.
Example 1: Reading Raw Data
In this example we’ll load a plain text file served in a remote location.
var planetsRequest = HttpRequest { location: "http://silveiraneto.net/downloads/planets"; onInput: function(stream: InputStream) { var buff = new BufferedReader(new InputStreamReader(stream)); var line = ""; while((line = buff.readLine())!=null){ println(line); } } } planetsRequest.enqueue();
This will produce the output:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Example 2: Discovering your IP Address
In this example we’ll examine how to integrate a request of a remote data in a running graphical program.
The best way to know your real IP address is asking for a remote server to look which IP made that request. It’s like calling for a friend and asking him which number appeared in his mobile. =) This server side Python script prints the IP address of who requested the page.
#!/usr/bin/env python import os print "Content-type: text/html" print print os.environ['REMOTE_ADDR']
In the client side, with JavaFX, we’ll load the remote value into a local variable. The ip is assigned with the value “…” and later the ipRequest will replace it with a String with the IP. The bind feature will automatically fix the GUI String text.
For the user he will see the ellipsis for a few seconds and so their IP.
import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.text.Text; import javafx.io.http.HttpRequest; import java.io.*; var ip = "..."; Stage { title: "What is my IP?" width: 250 height: 80 scene: Scene { content: Text { x: 10, y: 30 content: bind "My IP is {ip}" } } } var ipRequest = HttpRequest { location: "http://silveiraneto.net/scripts/myip.py"; onInput: function(stream: InputStream) { var buff = new BufferedReader(new InputStreamReader(stream)); ip = buff.readLine(); } } ipRequest.enqueue();
You can try this JavaFX applet here.
Example 3: Reading Integer values
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 WSDL file.
Here I’m writing a very simple service script at Zembly, a great platform for cloud computing. It’s called aplusb, it justs add the first parameter A to the second B.
if ((Parameters.a != null) && (Parameters.b!= 0)) { return Parameters.a+Parameters.b; }
The service is published at Zembly here where you can see more details on how to invoke it.
A simple way to invoke it on JavaFX and than getting the value as an Integer:
import java.io.*; import javafx.io.http.HttpRequest; var a = 100; var b = 200; var result = 0 on replace { println(result); } var zemblyRequest = HttpRequest { location: "http://zembly.net/things/1827f696529d4e6f940c36e8e79bea1c;exec?a={a}&b={b}"; onInput: function(stream: InputStream) { var buff = new BufferedReader(new InputStreamReader(stream)); result = Integer.valueOf(buff.readLine()); } } zemblyRequest.enqueue();
The output will be:
0
300
The first 0 is from the first assignment on the var result. The 300 is from the webservice itself.
The same approach can be used to convert the ASCII/Unicode result from the stream to the suitable type on a variable.
JavaFX, getting resources of inside your JAR
Apr 10th
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 { image: Image { url: "http://example.com/myPicture.png" } }
or a resource inside your own Jar file with the __DIR__ constant:
ImageView { image: Image { url: "{__DIR__}/myPicture.png" } }
But for other classes loading a internal resource (inside your own jarfile) is not so direct. For example, in the article Parsing a XML Sandwich with JavaFX I had to place the XML file in a temp directory. A more elegant way would be:
package handlexml; import java.io.FileInputStream; import javafx.data.pull.*; import javafx.ext.swing.*; import javafx.scene.Scene; import javafx.stage.Stage; class Resource{ function getUrl(name:String){ return this.getClass().getResource(name); } function getStream(name:String){ return this.getClass().getResourceAsStream(name); } } var list = SwingList { width: 600, height: 300} var myparser = PullParser { documentType: PullParser.XML; onEvent: function (e: Event) { var item = SwingListItem {text: "event {e}"}; insert item into list.items; } input: Resource{}.getStream("my.xml"); } myparser.parse(); Stage { title: "Map" scene: Scene { content: list } }
With a simple XML file called my.xml inside your package.
<?xml version="1.0" encoding="UTF-8"?> <bread sesame="true"> <catchup/> <hamburguer/> <cheese type="chedar"/> <maionese/> <lettuce/> </bread>

And we get the same result as before, but all files inside our Jars.

References:
- Hildeberto’s article Acessing Resources Inside of JAR Files
Parsing a XML Sandwich with JavaFX
Dec 25th

Let sandwich.xml be a file at /tmp directory with the content above.
<?xml version="1.0" encoding="UTF-8"?> <bread sesame="true"> <catchup/> <hamburguer/> <cheese type="chedar"/> <maionese/> <lettuce/> </bread>
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 and simple parser with a GUI that show the list of events during the parse process.
import java.io.FileInputStream; import javafx.data.pull.Event; import javafx.data.pull.PullParser; import javafx.ext.swing.SwingList; import javafx.ext.swing.SwingListItem; import javafx.scene.Scene; import javafx.stage.Stage; var list = SwingList { width: 600 height: 300 } var myparser = PullParser { documentType: PullParser.XML; onEvent: function (e: Event) { var item = SwingListItem { text: "event {e}" }; insert item into list.items; } input: new FileInputStream("/tmp/sandwich.xml"); } myparser.parse(); Stage { title: "XML Sandwich" scene: Scene { content: list } }

The XML cheese element produce two the outputs.
type:1 typeName:START_ELEMENT level:1 qname:cheese text:” namespaces:{} attributes:{type=chedar}
type:2 typeName:END_ELEMENT level:1 qname:cheese text:” namespaces:{} attributes:{type=chedar}
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’s contents, building a sandwich at runtime based on the XML file.
import java.io.FileInputStream; import javafx.data.pull.Event; import javafx.data.pull.PullParser; import javafx.data.xml.QName; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.scene.Scene; import javafx.stage.Stage; // my sandwich starts as an empty VBox var mysandwich = VBox {} // give a name and returns a ImageView with a png image like the name function ingredient(name){ return ImageView { image: Image { url: "{__DIR__}{name}.png" } } } // basicaly, look the event and put a ingredient at mysandwich var myparser = PullParser { documentType: PullParser.XML; onEvent: function (e: Event) { // starter xml elements if(e.type == PullParser.START_ELEMENT){ // bread if(e.qname.name.equals("bread")){ insert ingredient("bread_top") into mysandwich.content; } // hamburguer if(e.qname.name.equals("hamburguer")){ insert ingredient("hamburguer") into mysandwich.content; } // catchup if(e.qname.name.equals("catchup")){ insert ingredient("catchup") into mysandwich.content; } // maionese if(e.qname.name.equals("maionese")){ insert ingredient("maionese") into mysandwich.content; } // lettuce if(e.qname.name.equals("lettuce")){ insert ingredient("lettuce") into mysandwich.content; } // cheese if(e.qname.name.equals("cheese")){ var type= e.getAttributeValue(QName{name:"type"}); if(type.equals("cheedar")){ insert ingredient("cheedar") into mysandwich.content; } else { insert ingredient("cheese") into mysandwich.content; } } } // ending xml elements (just bread) if(e.type == PullParser.END_ELEMENT){ if(e.qname.name.equals("bread")){ insert ingredient("bread_botton") into mysandwich.content; } } } input: new FileInputStream("/tmp/sandwich.xml"); } myparser.parse(); Stage { title: "XML Sandwich" scene: Scene { height: 300 content: mysandwich } }
Here’s our sandwich.

Just changing the XML file you got a new sandwich.
<?xml version="1.0" encoding="UTF-8"?> <!-- double burger --> <bread sesame="true"> <maionese/> <lettuce/> <hamburguer/> <cheese type="chedar"/> <catchup/> <hamburguer/> <lettuce/> </bread>

Bon appétit.
For more details on XML and JSON parsing see the JavaFX API.
- ps: Sandwich image from commons.wikimedia.org
- ps2: We’re out of sesame.
- ps3: Sources, xmlsandwich.tar.bz2.
Tradução: há uma 










