<?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>Vicente Russo Neto &#187; XML</title>
	<atom:link href="http://www.vrusso.com.br/blog/category/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vrusso.com.br/blog</link>
	<description>Web Development</description>
	<lastBuildDate>Mon, 15 Aug 2011 13:43:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Working with large XML files in PHP</title>
		<link>http://www.vrusso.com.br/blog/2009/05/working-with-large-xml-files-in-php/</link>
		<comments>http://www.vrusso.com.br/blog/2009/05/working-with-large-xml-files-in-php/#comments</comments>
		<pubDate>Sun, 24 May 2009 00:25:20 +0000</pubDate>
		<dc:creator>Vicente Russo Neto</dc:creator>
				<category><![CDATA[Códigos]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xmlreader]]></category>

		<guid isPermaLink="false">http://www.vrusso.com.br/blog/?p=77</guid>
		<description><![CDATA[There are many ways to manage big XML files in php. When I say large, is really large, 10, 20, 30 mebabytes. If we were open a file like this on a common server, we certainly will have a <em>time-out</em> error. This is because we usually use <a href="http://us2.php.net/simplexml">SimpleXML</a> functions. This extensions is, in fact, a "Tree-based parser", just like "DOM Parser". They work great in small-medium XML files (~1MB). They put the whole content on memory, and then, parse it. But when we face a big content, the only option is the "Stream-based parsers". These are more efficiently and faster, because it read the file on demand, and don`t crush your server`s memory.</p>]]></description>
			<content:encoded><![CDATA[<p></p><p>There are many ways to manage big XML files in php. When I say large, is really large, 10, 20, 30 mebabytes. If we were open a file like this on a common server, we certainly will have a <em>time-out</em> error. This is because we usually use <a href="http://us2.php.net/simplexml">SimpleXML</a> functions. This extensions is, in fact, a &#8220;Tree-based parser&#8221;, just like &#8220;DOM Parser&#8221;. They work great in small-medium XML files (~1MB). They put the whole content on memory, and then, parse it. But when we face a big content, the only option is the &#8220;Stream-based parsers&#8221;. These are more efficiently and faster, because it read the file on demand, and don`t crush your server`s memory.</p>
<p>Amongst the stream-based parsers, we have SAX and <a href="http://www.php.net/xmlreader">XMLReader</a>. I`ll show you how to read a big XML with XMLReader, because it`s easier and faster comparing to SAX, as you can se <a href="http://php5.bitflux.org/php-eb2/title.php">here</a>.</p>
<p>The XMLReader is an extension enabled by default on PHP 5.1 and earlier. Born from XmlTextReader API (C#) and it`s based om libxml2 library. Before that, the XMLReader extension was only available on <a href="http://pecl.php.net/">PECL</a>. XMLReader supports namespaces and validations, including DTD and Relax NG (REgular LAnguage for XML Next Generation).</p>
<p>Well, lets to the code. Here`s me XML example:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;users<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>John<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My Address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;zipcode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>12345<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/zipcode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;city<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My City<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/city<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>555 1234-4321<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/users<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>PHP code:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeType</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #009900;">&#40;</span>XMLReader<span style="color: #339933;">::</span><span style="color: #004000;">ELEMENT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">localName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$reader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DomDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">importNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$simple_xml</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/simplexml_import_dom"><span style="color: #990000;">simplexml_import_dom</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$simple_xml</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$simple_xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$address</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$simple_xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">address</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Custom code insert, update, whatever...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>See, in determinate point I transform the actual block of data (the <em>users</em> tag) into a SimpleXML object, making the manipulation extremely easy. This way, you can easily work with big XML with no harm to the server`s memory and no decrease on speed. My next move: try to optimize this script and maybe create a <a href="http://www.codeigniter.com">CodeIgniter</a>`s library. That certainly will be a nice addon to developers, I think. Of course any help will be very welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vrusso.com.br/blog/2009/05/working-with-large-xml-files-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Manipulando XML em grande escala com PHP</title>
		<link>http://www.vrusso.com.br/blog/2009/05/manipulando-xml-em-grande-escala-com-php/</link>
		<comments>http://www.vrusso.com.br/blog/2009/05/manipulando-xml-em-grande-escala-com-php/#comments</comments>
		<pubDate>Sat, 23 May 2009 22:48:17 +0000</pubDate>
		<dc:creator>Vicente Russo Neto</dc:creator>
				<category><![CDATA[Códigos]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xmlreader]]></category>

		<guid isPermaLink="false">http://www.vrusso.com.br/blog/?p=68</guid>
		<description><![CDATA[Existem vários métodos para manipular arquivos XML grandes. Quando digo grandes, são realmente grandes, 10, 20, 30 mega. Se fossemos abrir um arquivo desses em um servidor comum, certamente a operação ia terminar com um &#8220;time-out&#8221;. Isso porque geralmente usamos as funcoes da extensão SimpleXML. Esta extensão tem um &#8220;Tree-based parser&#8221;, assim como o &#8220;DOM [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Existem vários métodos para manipular arquivos XML grandes. Quando digo grandes, são realmente grandes, 10, 20, 30 mega. Se fossemos abrir um arquivo desses em um servidor comum, certamente a operação ia terminar com um &#8220;time-out&#8221;. Isso porque geralmente usamos as funcoes da extensão <a href="http://us2.php.net/simplexml">SimpleXML</a>. Esta extensão tem um &#8220;Tree-based parser&#8221;, assim como o &#8220;DOM Parser&#8221;. Funcionam perfeitamente em arquivos pequenos. Estes jogam o conteúdo do XML em memória, e dali você manipula. Mas quando os arquivos são muito grandes, o negócio é procurar um Stream-based Parser. São mais eficientes pois fazem a leitura do arquivo sob demanda, é mais rápido e não mastiga a memória do servidor.</p>
<p>Dentre os Stream-based Parsers, temos o SAX e o <a href="http://www.php.net/xmlreader">XMLReader</a>. Vou demostrar como fazer a leitura de um XML utilizando o XMLReader, pois é mais fácil de implementar e de execução mais rápida, como podem acompanhar <a href="http://php5.bitflux.org/php-eb2/title.php">neste link</a>.</p>
<p>O XMLReader é uma extensão habilitada e incluída por padrão a partir da versao 5.1 do PHP, surgiu através da derivação da API do XmlTextReader em C# e é baseada na biblioteca libxml2. Antes disso, a extensão XMLReader era disponível apenas na <a href="http://pecl.php.net/">PECL</a>. O XMLReader suporta namespaces e validações, incluindo DTD e Relax NG (REgular LAnguage for XML Next Generation)</p>
<p>Bom, vamos ao código. Meu XML de exemplo tem a seguinte estrutura:</p>
<p><div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vendedor</span> <span style="color: #000066;">codigo</span>=<span style="color: #ff0000;">&quot;000001&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>João Da Silva<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endereco<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Avenida São Paulo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/endereco<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bairro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Centro<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bairro<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <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><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cep<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>xx.xx-xx<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cep<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;telefone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>(11) 1234-4321<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/telefone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cpf<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>12345678901<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cpf<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1234567890<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vendedor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vendedor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
.<br />
.<br />
.<br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vendedor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
</p>
<p>Código em PHP:</p>
<p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$vendedores</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$vendedores</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vendedores.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vendedores</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vendedores</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeType</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #009900;">&#40;</span>XMLReader<span style="color: #339933;">::</span><span style="color: #004000;">ELEMENT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vendedores</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">localName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;vendedor&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vendedores</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DomDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">importNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$simple_xml</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/simplexml_import_dom"><span style="color: #990000;">simplexml_import_dom</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$codigo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$simple_xml</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'codigo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$nome</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$simple_xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nome</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$endereco</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$simple_xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">endereco</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Código customizado... insert, update, etc.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</p>
<p> </p>
<p>Percebam que em um determinado momento eu transformo o bloco de leitura atual, ou seja, a tag <em>vendedores</em> em um objeto SimpleXML, tornando a leitira extremamente fácil. Desta maneira voce pode tranquilamente trabalhar com arquivos XML de 5, 10, 50 mega sem detonar a memória do servidor. Meu próximo passo será melhorar essa rotina e transforma-la em uma library pro framework <a href="http://www.codeigniter.com">CodeIgniter</a>. A quem possa interessar, toda ajuda é bem vinda!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vrusso.com.br/blog/2009/05/manipulando-xml-em-grande-escala-com-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

