<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How-to Handle Large XML Files in PHP</title>
	<atom:link href="http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/</link>
	<description>Tech-Related Bloggery</description>
	<lastBuildDate>Thu, 09 Sep 2010 10:18:25 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Francisco</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-1016</link>
		<dc:creator>Francisco</dc:creator>
		<pubDate>Fri, 02 Jul 2010 12:01:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-1016</guid>
		<description>Olá,

Tenho o seguinte código abaixo e gostaria de saber como faço um laço para ler diverços xmls em um diretório para ir gravando no banco um a um.



session_start();
include(&#039;inc/head.php&#039;);
include(&#039;banco.php&#039;); 

$xml = array();
$xml_elem = null;
$contador=1;  
function startElement( $parser, $nome, $attrs ){
	global $xml, $xml_elem;
	$xml_elem = $nome;
}

function endElement( $parser, $nome ){
	global $xml_elem;
	if  ( $nome == &#039;LIVRO&#039;) {echo &#039;contador = &#039;.$contador++;}
	$xml_elem = null;
}

function dadosXML( $parser, $texto ){
	global $xml, $xml_elem,$contador;
	if ( $xml_elem == &#039;ID&#039; &#124;&#124; $xml_elem == &#039;TITULO&#039; &#124;&#124; $xml_elem == &#039;AUTOR&#039; &#124;&#124; $xml_elem == &#039;DATA&#039; ){
		$xml[$contador][$xml_elem]=$texto;
		if ( $xml_elem == &#039;DATA&#039;){$contador++;}
	}
}
$parser = xml_parser_create();  
xml_set_element_handler( $parser, &quot;startElement&quot;, &quot;endElement&quot; );
xml_set_character_data_handler( $parser, &quot;dadosXML&quot; );
$file = fopen( &#039;livros.xml&#039;, &#039;r&#039; );
while( $dados = fread( $file, 4096 ) ){
	xml_parse( $parser, $dados );
	echo fgets($file).&#039;Teste de Leitura XML.&#039;;
}
xml_parser_free( $parser );
$pLinha = &#039;&#039;;

if ($open){
  inicializaProcesso($open);
   $ID=&quot;&quot;;$TITULO=&quot;&quot;;$AUTOR=&quot;&quot;;$DATA =&quot;&quot;;
   foreach( $xml as $contXml ){
  	echo &#039;ID = &#039;.$contXml[&#039;ID&#039;];
	echo &#039;TITULO = &#039;.$contXml[&#039;TITULO&#039;];	
	echo &#039;AUTOR = &#039;.$contXml[&#039;AUTOR&#039;];
	echo &#039;DATA = &#039;.$contXml[&#039;DATA&#039;];
        $sql =  &quot;INSERT INTO FVS_lib(id,titulo,autor,data)
		VALUES (&quot;.$contXml[&#039;ID&#039;].&quot;,&#039;&quot;.$contXml[&#039;TITULO&#039;].&quot;&#039;,&#039;&quot;.$contXml[&#039;AUTOR&#039;].&quot;&#039;,&#039;&quot;.$contXml[&#039;DATA&#039;].&quot;&#039;);&quot;;				      
				echo &#039;passei por aqui&#039;.$contXml[&#039;ID&#039;];
	    $query   =    ifx_prepare($sql, $open);
	    $result  =    ifx_do($query);
		if (!$result){
			echo &quot;Nenhum dado foi gravado&quot;;
			exit;
		}else{ echo &quot;Dados gravados com sucesso&quot;;}		
		
}
  finalizaProcesso($open);
  echo &quot;CONEXÃO COM O DB REALIZADA COM SUCESSO&quot;;
}else{ echo &#039;NAO CONECTADO&#039;; } 


foreach( $xml as $contXml ){
	echo $pLinha.&quot;&lt;b&gt;&quot;.
	$contXml[&#039;ID&#039;].&quot;&lt;/b&gt;&quot;.
	$contXml[&#039;TITULO&#039;].&quot;&lt;/b&gt;&quot;.	
	$contXml[&#039;AUTOR&#039;].&quot;&quot;.
	$contXml[&#039;DATA&#039;].&quot;&quot;;
	&#039;DATA&#039;.&quot;&quot;;
}

function stringSql(){  
	global $xml;
	foreach( $xml as $contXml ){
		$sql =  &quot;INSERT INTO FVS_lib(id,titulo,autor,data)
	    VALUES (&quot;.$contXml[&#039;ID&#039;].&quot;,&quot;.$contXml[&#039;TITULO&#039;].&quot;,&quot;.$contXml[&#039;AUTOR&#039;].&quot;,&quot;.$contXml[&#039;DATA&#039;].&quot;);&quot;;
		//echo &#039;passei por aqui&#039;.$contXml[&#039;ID&#039;];
	    $query   =    ifx_prepare($sql, $conexao);
	    $result  =    ifx_do($query);

	}
	return $sql;
}
function insertCont($conexao){
  $id = 12;$titulo = &quot;rteterter&quot;;$autor = &quot;yereryre&quot;;$data = &quot;reyeewre&quot;;		
  $query   =    ifx_prepare(&quot;SELECT * FROM FVS_lib&quot;, $conexao);
  $result  =    ifx_do($query);
	while($arr1 = ifx_fetch_row($query)){
		$id				=	$arr1[&#039;id&#039;];
		$titulo	  		=	$arr1[&#039;titulo&#039;];
		$autor 			=   $arr1[&#039;autor&#039;];
		$data	 		=   $arr1[&#039;data&#039;];
        echo &quot;alert(&#039;Salvo com Sucesso!&#039;);&quot;;				
	}
	if ($id == 0){	
		$exist .= &quot;Nenhum registro encontrado.&quot;;
		echo $exist;	
	}
    $printResult = imprimeInf($id,$titulo,$autor,$data);
	echo $printResult.&#039;&#039;;
	echo $mensagem.&#039;-----------------&#039;;
	
}

function imprimeInf($id,$titulo,$autor,$data){	
  $string .= &quot;&lt;b&gt;ID :  $id &lt;/b&gt;&quot;;	
  $string .= &quot;&lt;b&gt;Titulo : $titulo&lt;/b&gt;&quot;;
  $string .= &quot;&lt;b&gt;Autor : $autor&lt;/b&gt;&quot;;
  $string .= &quot;&lt;b&gt;Data : $data&lt;/b&gt;&quot;;
  return $string;
  
  }</description>
		<content:encoded><![CDATA[<p>Olá,</p>
<p>Tenho o seguinte código abaixo e gostaria de saber como faço um laço para ler diverços xmls em um diretório para ir gravando no banco um a um.</p>
<p>session_start();<br />
include(&#8217;inc/head.php&#8217;);<br />
include(&#8217;banco.php&#8217;); </p>
<p>$xml = array();<br />
$xml_elem = null;<br />
$contador=1;<br />
function startElement( $parser, $nome, $attrs ){<br />
	global $xml, $xml_elem;<br />
	$xml_elem = $nome;<br />
}</p>
<p>function endElement( $parser, $nome ){<br />
	global $xml_elem;<br />
	if  ( $nome == &#8216;LIVRO&#8217;) {echo &#8216;contador = &#8216;.$contador++;}<br />
	$xml_elem = null;<br />
}</p>
<p>function dadosXML( $parser, $texto ){<br />
	global $xml, $xml_elem,$contador;<br />
	if ( $xml_elem == &#8216;ID&#8217; || $xml_elem == &#8216;TITULO&#8217; || $xml_elem == &#8216;AUTOR&#8217; || $xml_elem == &#8216;DATA&#8217; ){<br />
		$xml[$contador][$xml_elem]=$texto;<br />
		if ( $xml_elem == &#8216;DATA&#8217;){$contador++;}<br />
	}<br />
}<br />
$parser = xml_parser_create();<br />
xml_set_element_handler( $parser, &#8220;startElement&#8221;, &#8220;endElement&#8221; );<br />
xml_set_character_data_handler( $parser, &#8220;dadosXML&#8221; );<br />
$file = fopen( &#8216;livros.xml&#8217;, &#8216;r&#8217; );<br />
while( $dados = fread( $file, 4096 ) ){<br />
	xml_parse( $parser, $dados );<br />
	echo fgets($file).&#8217;Teste de Leitura XML.&#8217;;<br />
}<br />
xml_parser_free( $parser );<br />
$pLinha = &#8221;;</p>
<p>if ($open){<br />
  inicializaProcesso($open);<br />
   $ID=&#8221;";$TITULO=&#8221;";$AUTOR=&#8221;";$DATA =&#8221;";<br />
   foreach( $xml as $contXml ){<br />
  	echo &#8216;ID = &#8216;.$contXml['ID'];<br />
	echo &#8216;TITULO = &#8216;.$contXml['TITULO'];<br />
	echo &#8216;AUTOR = &#8216;.$contXml['AUTOR'];<br />
	echo &#8216;DATA = &#8216;.$contXml['DATA'];<br />
        $sql =  &#8220;INSERT INTO FVS_lib(id,titulo,autor,data)<br />
		VALUES (&#8221;.$contXml['ID'].&#8221;,&#8217;&#8221;.$contXml['TITULO'].&#8221;&#8216;,&#8217;&#8221;.$contXml['AUTOR'].&#8221;&#8216;,&#8217;&#8221;.$contXml['DATA'].&#8221;&#8216;);&#8221;;<br />
				echo &#8216;passei por aqui&#8217;.$contXml['ID'];<br />
	    $query   =    ifx_prepare($sql, $open);<br />
	    $result  =    ifx_do($query);<br />
		if (!$result){<br />
			echo &#8220;Nenhum dado foi gravado&#8221;;<br />
			exit;<br />
		}else{ echo &#8220;Dados gravados com sucesso&#8221;;}		</p>
<p>}<br />
  finalizaProcesso($open);<br />
  echo &#8220;CONEXÃO COM O DB REALIZADA COM SUCESSO&#8221;;<br />
}else{ echo &#8216;NAO CONECTADO&#8217;; } </p>
<p>foreach( $xml as $contXml ){<br />
	echo $pLinha.&#8221;<b>&#8220;.<br />
	$contXml['ID'].&#8221;</b>&#8220;.<br />
	$contXml['TITULO'].&#8221;&#8220;.<br />
	$contXml['AUTOR'].&#8221;".<br />
	$contXml['DATA'].&#8221;";<br />
	&#8216;DATA&#8217;.&#8221;";<br />
}</p>
<p>function stringSql(){<br />
	global $xml;<br />
	foreach( $xml as $contXml ){<br />
		$sql =  &#8220;INSERT INTO FVS_lib(id,titulo,autor,data)<br />
	    VALUES (&#8221;.$contXml['ID'].&#8221;,&#8221;.$contXml['TITULO'].&#8221;,&#8221;.$contXml['AUTOR'].&#8221;,&#8221;.$contXml['DATA'].&#8221;);&#8221;;<br />
		//echo &#8216;passei por aqui&#8217;.$contXml['ID'];<br />
	    $query   =    ifx_prepare($sql, $conexao);<br />
	    $result  =    ifx_do($query);</p>
<p>	}<br />
	return $sql;<br />
}<br />
function insertCont($conexao){<br />
  $id = 12;$titulo = &#8220;rteterter&#8221;;$autor = &#8220;yereryre&#8221;;$data = &#8220;reyeewre&#8221;;<br />
  $query   =    ifx_prepare(&#8221;SELECT * FROM FVS_lib&#8221;, $conexao);<br />
  $result  =    ifx_do($query);<br />
	while($arr1 = ifx_fetch_row($query)){<br />
		$id				=	$arr1['id'];<br />
		$titulo	  		=	$arr1['titulo'];<br />
		$autor 			=   $arr1['autor'];<br />
		$data	 		=   $arr1['data'];<br />
        echo &#8220;alert(&#8217;Salvo com Sucesso!&#8217;);&#8221;;<br />
	}<br />
	if ($id == 0){<br />
		$exist .= &#8220;Nenhum registro encontrado.&#8221;;<br />
		echo $exist;<br />
	}<br />
    $printResult = imprimeInf($id,$titulo,$autor,$data);<br />
	echo $printResult.&#8221;;<br />
	echo $mensagem.&#8217;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8217;;</p>
<p>}</p>
<p>function imprimeInf($id,$titulo,$autor,$data){<br />
  $string .= &#8220;<b>ID :  $id </b>&#8220;;<br />
  $string .= &#8220;<b>Titulo : $titulo</b>&#8220;;<br />
  $string .= &#8220;<b>Autor : $autor</b>&#8220;;<br />
  $string .= &#8220;<b>Data : $data</b>&#8220;;<br />
  return $string;</p>
<p>  }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Roman</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-741</link>
		<dc:creator>Vincent Roman</dc:creator>
		<pubDate>Fri, 05 Feb 2010 12:20:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-741</guid>
		<description>Christian - 

Nice post and an elegant solution.  Alas in my real world scenario and based on restrictions, I couldn&#039;t use it.  

Yours is one way of many and thanks so much ofr sharing :)  

Best, Vincent -</description>
		<content:encoded><![CDATA[<p>Christian &#8211; </p>
<p>Nice post and an elegant solution.  Alas in my real world scenario and based on restrictions, I couldn&#8217;t use it.  </p>
<p>Yours is one way of many and thanks so much ofr sharing :)  </p>
<p>Best, Vincent -</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Weiske</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-740</link>
		<dc:creator>Christian Weiske</dc:creator>
		<pubDate>Thu, 04 Feb 2010 13:49:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-740</guid>
		<description>You are circumventing the problem here. PHP has much more elegant ways to deal with huge XML files. I described one in a blog entry:
http://cweiske.de/tagebuch/Importing%20huge%20XML%20files%20using%20PHP5%20-%20efficiently%20and%20conveniently.htm</description>
		<content:encoded><![CDATA[<p>You are circumventing the problem here. PHP has much more elegant ways to deal with huge XML files. I described one in a blog entry:<br />
<a href="http://cweiske.de/tagebuch/Importing%20huge%20XML%20files%20using%20PHP5%20-%20efficiently%20and%20conveniently.htm" rel="nofollow">http://cweiske.de/tagebuch/Importing%20huge%20XML%20files%20using%20PHP5%20-%20efficiently%20and%20conveniently.htm</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-626</link>
		<dc:creator>Vincent</dc:creator>
		<pubDate>Wed, 02 Dec 2009 10:02:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-626</guid>
		<description>No problem, glad the notion and technique is of use as much as the code.  No doubt I would refactor the code if I needed it again :)  Best, Vincent -</description>
		<content:encoded><![CDATA[<p>No problem, glad the notion and technique is of use as much as the code.  No doubt I would refactor the code if I needed it again :)  Best, Vincent -</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cranbow</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-625</link>
		<dc:creator>cranbow</dc:creator>
		<pubDate>Wed, 02 Dec 2009 09:54:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-625</guid>
		<description>Thanks for the tips. This helped me a lot. I cleaned up the code a bit for my purposes, but it worked very well.</description>
		<content:encoded><![CDATA[<p>Thanks for the tips. This helped me a lot. I cleaned up the code a bit for my purposes, but it worked very well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: webdesign</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-568</link>
		<dc:creator>webdesign</dc:creator>
		<pubDate>Fri, 25 Sep 2009 13:19:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-568</guid>
		<description>Great tutorial .. you missed alot of syntax but when i finally got it runningno more big file errors :D</description>
		<content:encoded><![CDATA[<p>Great tutorial .. you missed alot of syntax but when i finally got it runningno more big file errors :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Chan</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-548</link>
		<dc:creator>Jim Chan</dc:creator>
		<pubDate>Thu, 23 Jul 2009 09:39:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-548</guid>
		<description>HeyThanks for sharing the great piece of code. Just have a question and hope if you could please address this question. I think I have misconfigured the file.

Here is an example for items: Which one is core data delimiter / record delimeter? 

 




New York
Yes






New York
Yes





Thanks again!
-Jim</description>
		<content:encoded><![CDATA[<p>HeyThanks for sharing the great piece of code. Just have a question and hope if you could please address this question. I think I have misconfigured the file.</p>
<p>Here is an example for items: Which one is core data delimiter / record delimeter? </p>
<p>New York<br />
Yes</p>
<p>New York<br />
Yes</p>
<p>Thanks again!<br />
-Jim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travis Ballard</title>
		<link>http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/comment-page-1/#comment-496</link>
		<dc:creator>Travis Ballard</dc:creator>
		<pubDate>Sat, 09 May 2009 06:36:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.techtalkpoint.com/?p=100214#comment-496</guid>
		<description>i just wanted to thank you for this. it helped me quite a bit with a project i just finished. at first i tried just using simplexml to read a 400mb xml file and well, php wasn&#039;t having that. now it may create 20k files but it deletes them when it&#039;s done and everything is working great. thanks!</description>
		<content:encoded><![CDATA[<p>i just wanted to thank you for this. it helped me quite a bit with a project i just finished. at first i tried just using simplexml to read a 400mb xml file and well, php wasn&#8217;t having that. now it may create 20k files but it deletes them when it&#8217;s done and everything is working great. thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
