<?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>Technócil &#187; simplexml</title>
	<atom:link href="http://technocil.com/tag/simplexml/feed/" rel="self" type="application/rss+xml" />
	<link>http://technocil.com</link>
	<description></description>
	<lastBuildDate>Tue, 24 Aug 2010 21:24:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dica Rapida: Pegando Twittadas com PHP</title>
		<link>http://technocil.com/2009/04/24/dica-rapida-pegando-twittadas-com-php-2/</link>
		<comments>http://technocil.com/2009/04/24/dica-rapida-pegando-twittadas-com-php-2/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 14:25:42 +0000</pubDate>
		<dc:creator>João Vagner</dc:creator>
				<category><![CDATA[Desenvolvimento web]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://technocil.com/2009/04/24/dica-rapida-pegando-twittadas-com-php-2/</guid>
		<description><![CDATA[Ola a todos,
Vou começar uma série de posts rapidos com dicas legais e uteis pro dia-a-dia relacionadadas a PHP.
Quase todos usam Twitter, e ja sabemos o o Twitter tem uma API para integração, porem nao e necessario usar uma API para algo basico, como pegar os Ultimos posts de alguem via RSS. Ja que RSS [...]]]></description>
			<content:encoded><![CDATA[<p>Ola a todos,</p>
<p>Vou começar uma série de posts rapidos com dicas legais e uteis pro dia-a-dia relacionadadas a PHP.</p>
<p>Quase todos usam Twitter, e ja sabemos o o Twitter tem uma API para integração, porem nao e necessario usar uma API para algo basico, como pegar os Ultimos posts de alguem via RSS. Ja que RSS e um formato XML.</p>
<p>Abaixo um codigo simples e rapido:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$rss</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://twitter.com/statuses/user_timeline/14627317.rss'</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$abre</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rss</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">;</span> 
<span style="color: #000088;">$limite</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$abre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$itens</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">utf8_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$itens</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$post</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;hr /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">==</span><span style="color: #000088;">$limite</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>$rss</strong><br />
Indica o caminho completo do RSS a ser lido.</p>
<p><strong>$abre</strong><br />
Utiliza simplexml_load_file para ler o xml e converter em um objeto/p</p>
<p><strong>$i</strong><br />
Define o inicio do loop para que possamos frear o loop no nosso limite de twittadas./p</p>
<p><strong>$limite=3</strong><br />
Definimos um limite de posts, no caso como nosso loop comeca em em 0, definindo 3, o limite de posts seria 4: 0,1,2,3</p>
<p><strong>foreach ($abre&#8211;>channel->item as $itens ) {</strong><br />
Aqui criamos um loop do item do XML e o chamamos de $itens para melhor identificação dentro do loop.  Aqui ele herda os itens de channel->item, como title, e outros</p>
<p><strong>$post = utf8_decode($itens->title);</strong><br />
Aqui usamos o UTF-8 para decodificar as mensagens evitando erros de charset e podemos interpretar para o ISO. </p>
<p><strong>echo $post .&#8217; < hr / >&#8216;;</strong><br />
Aqui imprimos o Post na tela e adicionamos a tag<br />
<hr /> para separar os posts, criando uma linha de separação.</p>
<p><strong>if($i==$limite) { break; }</strong><br />
Aqui verificamos se o nosso loop chegou ao nosso limite definido acima, se chegar coloca um fim no loop do channel->item</p>
<p>Por fim e isso, basta usar em seus blogs, e onde mais desejar ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://technocil.com/2009/04/24/dica-rapida-pegando-twittadas-com-php-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leitura de XML &#8211; Parte 2 SimpleXML</title>
		<link>http://technocil.com/2008/12/24/leitura-de-xml-parte-2-simplexml/</link>
		<comments>http://technocil.com/2008/12/24/leitura-de-xml-parte-2-simplexml/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 13:00:14 +0000</pubDate>
		<dc:creator>João Vagner</dc:creator>
				<category><![CDATA[Desenvolvimento web]]></category>
		<category><![CDATA[Destaques]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://technocil.com/2008/12/24/leitura-de-xml-parte-2-simplexml/</guid>
		<description><![CDATA[Olá Pessoal,
Primeiramente, post de dia 24 de Dezembro, desejos a todos um Feliz Natal, que estejam todos ai junto a amigos e familiares curtindo uma data especial.
Bom, demorei um pouco, mas chegou, a segunta parte de Leitura de XML usando SimpleXML, dessa vez vou usar alguns modelos, ao invez de trabalhar com o nosso Feed [...]]]></description>
			<content:encoded><![CDATA[<p>Olá Pessoal,</p>
<p>Primeiramente, post de dia 24 de Dezembro, desejos a todos um Feliz Natal, que estejam todos ai junto a amigos e familiares curtindo uma data especial.</p>
<p>Bom, demorei um pouco, mas chegou, a segunta parte de Leitura de XML usando SimpleXML, dessa vez vou usar alguns modelos, ao invez de trabalhar com o nosso Feed de RSS, vamos trabalhar com valores de cambio e pesquisa de CEP. Tudo simples e em poucas linhas.</p>
<p>Primeiramente precisamos de algum XML que seja atualizado, com os valores que temos que trabalhar. Para isso existe esse endereço: <a href="http://www.republicavirtual.com.br/cotacao/exemplos.php">http://www.republicavirtual.com.br/cotacao/exemplos.php</a></p>
<p>Não posso garantir 100% de uptime do link, pois não sei se algo vai mudar ok? Mas iremos trabalhar em cima dos retornos desse xml, ok?</p>
<h3>Simple XML</h3>
<p>O objetivo é fornecer acesso aos documentos XML usando propriedades de objetos e iterators, não é uma extensão complicada (são poucos métodos) mas é bastante flexível. </p>
<h3>Exemplo Básico</h3>
<p><strong>Importando o XML da URL: </strong><br />
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://cotacao.republicavirtual.com.br/web_cotacao.php?formato=xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h3>Iniciando o trabalho</h3>
<p>Aqui vamos pegar o valor do dolar comercial para compras:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// importando o arquivo</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://cotacao.republicavirtual.com.br/web_cotacao.php?formato=xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// pegando a tag dolar_commercial_compra</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dolar_comercial_compra</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As tags que podemos trabalhar podem ser encontrada nesse XML</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webservicecotacao<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dolar_comercial_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2,1260<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dolar_comercial_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dolar_comercial_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2,1280<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dolar_comercial_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dolar_paralelo_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2,2200<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dolar_paralelo_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dolar_paralelo_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2,3000<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dolar_paralelo_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;euro_dolar_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1,2410<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/euro_dolar_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;euro_dolar_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1,2410<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/euro_dolar_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;euro_real_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2,6384<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/euro_real_compra<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;euro_real_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2,6408<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/euro_real_venda<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webservicecotacao<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
 Viram como é simples trabalhar com o SimpleXML? Agora vamos trabalhar para achar endereços de CEP ;) O XML que iremos trabalhar emcima é desse modelo:</p>
<h2>Trabalhando com CEP</h3>
<p>Código: </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="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;iso-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webservicecep<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resultado<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resultado<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resultado_txt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>sucesso - cep completo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resultado_txt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uf<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SP<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/uf<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cidade<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>São Paulo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cidade<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bairro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Santana<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bairro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tipo_logradouro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rua<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tipo_logradouro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;logradouro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Duarte de Azevedo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/logradouro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webservicecep<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>A URL que iremos importar as consultas é: http://cep.republicavirtual.com.br/web_cep.php?cep=02036021&amp;formato=xml  </p>
<h3> Vamos ao Código:</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://cep.republicavirtual.com.br/web_cep.php?cep=02036021&amp;formato=xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;UF: &quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uf</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;Cidade: &quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cidade</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;Bairro: &quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bairro</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;Tipo de Logradouro: &quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tipo_logradouro</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;Endereço: &quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logradouro</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Não recomendo que essa aplicação rode atravez da url de pesquisa, a base de dados pode ser baixada nesse endereço: <a href="http://www.republicavirtual.com.br/cep/download/cep.sql.bz2">Download</a></p>
<p>É isso, viram como é simples integrar uma pesquisa de CEP pra completar formulários de cadastros e tudo mais? Agora é com vocês ;) </p>
]]></content:encoded>
			<wfw:commentRss>http://technocil.com/2008/12/24/leitura-de-xml-parte-2-simplexml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
