I'm using simplexml_load_file
to parse an XML document, but the file has more than 100 mb and instead of loading only part of the nodes, what's happening is that the whole document is being loaded from a before the nodes are extracted, and this is giving TimeOut
, taking more than 30 seconds to load the page, and at the end giving error.
The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Boutique</title>
</head>
<body>
<?php header ('Content-type: text/html; charset=UTF-8'); ?>
<!-- --><link rel="stylesheet" href="/va/artigos-complexos/afilio/afilio-vitrine.css" type="text/css" />
<div><p class="ofertasdotopovitrine">Conheça nossas super ofertas!</p>
</div>
<div class="mainproductebayfloatright-bottom">
<?php
function parse($url, $offset = 1, $limit = -1)
{
$xml = simplexml_load_file($url);
$limitCriteria = '';
if ($limit > 0) {
$limitCriteria = 'and position() <= ' . ((int)$offset + (int)$limit + 1);
}
$products = array();
$path = sprintf('//produto[position() >= %s %s]', (int)$offset, $limitCriteria);
foreach($xml->xpath($path) as $product) {
$products[] = array(
'nome' => $product->nome,
'preco_promocao' => $product->preco_promocao,
'description' => $product->descricao,
'link_imagem' => $product->link_imagem,
'link_produto' => $product->link_produto,
'preco_normal' => $product->preco_normal,
'parcelas' => $product->parcelas,
'vl_parcelas' => $product->vl_parcelas
);
}
return $products;
}
$products = parse('http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML', 5, 5);
?>
<?php
foreach ($products as $product) {
print '<div class="aroundebay"><div id="aroundebay2">';
/* */
print '<div class="titleebay"><a target="_blank" rel="nofollow" href="'. $product['link_produto'] . '">' . $product['nome'] . '"</a></div>';
print '<div class="mainproduct"><a target="_blank" rel="nofollow" href="' . $product['link_produto'] . '"><img style="height:120px" src="' . $product['link_imagem'] . '"/><br/>';
//print "De:; R$". $preco_normal . "<br/>";
print '<span>Apenas R$' . $product['preco_promocao'] . '<br/></a></span></div>';
//print "Em " . $parcelas . "x de : R$" . $vl_parcelas . "</a></span></div>";
print '</div></div>';
}
?>
</div>
</body>
</html>
css has no relevance.
If you want to change the XML URL to one of a small XML, just to test the code, use this:
http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895835&token=53e355b0a09ea0.74300807&progid=1681&format=XML
Would not it be possible to load only the first 10 Nodes, for example, without having to load the entire document?
If someone has a solution that uses jQuery
, I also accept it.
Thank you in advance.