Tenho utilizado o SimpleXMLReader para fazer parse de uma estrutura XML gigantesca, que possui mais de 25mb.
Meu código funciona normalmente, porém, estou com dois problemas:
Não consigo transformar o
Xpath
emHTML
. Bom, eu dei uma estudada nisso, e vi que não é uma tarefa fácil, é possível mesmo fazer isso? Acontece que o Xpath não permite que eu utilize os valores dentro de uma string PHP no código HTML.Não consigo limitar a quantidade de itens exibidos. Eu já olhei todo o código do arquivo simpleXMLreader.php, mas não vejo nenhum meio de limitar a quantidade de itens extraídos do XML, me disseram para utilizar
foreach
junto aoXpath
no código principal, eu tentei, mas não funcionou.
Baixe o software SimpleXMLReader aqui: https://github.com/dkrnl/SimpleXMLReader
Meu código:
<!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 class="mainproductebayfloatright-bottom">
<?php
require_once dirname(__FILE__). "/simplexmlreader.php";
class ExampleXmlReader1 extends SimpleXMLReader
{
public function __construct()
{
// by node name
$this->registerCallback("nome", array($this, "callbackNome"));
$this->registerCallback("preco_promocao", array($this, "callbackPrice"));
}
protected function callbackNome($reader)
{
$xml = $reader->expandSimpleXml();
$name = $xml;
$xpath = $this->currentXpath();
echo "$xpath: Nome = $name;\n";
return true;
}
protected function callbackPrice($reader)
{
$xml = $reader->expandSimpleXml();
$preco_promocao = $xml;
$xpath = $this->currentXpath();
echo "$xpath: Preço = $preco_promocao;\n";
return true;
}
}
echo "<pre>";
?>
<div class="aroundebay">
<div id="aroundebay2">
<?php
print "<div class=\"titleebay\"><a rel=\"nofollow\" href=\"$link_produto\">" . $title . "</a></div>";
print "<div class=\"mainproduct\"><a rel=\"nofollow\" href=\"$link\"><img style=\"height:120px\" src=\"$imagem\"/><br/>";
//print "De:; R$". $preco_normal . "<br/>";
print "<span>Apenas R$" . $preco_promocao . "<br/></a></span></div>";
//print "Em " . $parcelas . "x de : R$" . $vl_parcelas . "</a></span></div>";
?>
</div>
</div>
</div>
<?php
//Pega o arquivo pelo caminho relativo
//$file = dirname(__FILE__) . "/boutique.xml";
$reader = new ExampleXmlReader1;
// Pega o arquivo pela URL. Original: $reader->open($file);
$reader->open("http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML");
$reader->parse();
$reader->close();
?>
</body>
</html>
PS: O CSS não é relevante.