3
I’ve been using the Simplexmlreader to parse a gigantic XML structure, which has more than 25mb.
My code works normally, but I’m having two problems:
I can’t turn the
Xpath
inHTML
. Well, I’ve studied this, and I’ve seen that it’s not an easy task, is it even possible to do that? It turns out that Xpath does not allow me to use the values within a PHP string in HTML code.I can’t limit the amount of items displayed. I have already looked at all the code of the simpleXMLreader.php file, but I see no way to limit the amount of items extracted from XML, told me to use
foreach
next to theXpath
in the main code, I tried, but it didn’t work.
Download the Simplexmlreader software here: https://github.com/dkrnl/SimpleXMLReader
My code:
<!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: CSS is not relevant.
But this works well with the giant file?
– user013314
The file I downloaded, from the link of your code, is 66 MB. It took 1.5 seconds to process the entire file in my notebook.
– Begnini
All right, what about the HTML part? This solution of yours is completely new code or is it an implement?
– user013314
Now just put your HTML inside the foreach, a table, a list, and it goes on as you want to show the data. I don’t understand the question about the code.
– Begnini
Man, I’m not getting it.
– user013314
What can’t you get yet? Where are you stalling?
– Begnini
You developed a solution up there, I tried to throw it into my code and it didn’t work. Or this solution you developed is to be used separately?
– user013314
I understand how your code works. But can you put it to work with this XML
http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML
?– user013314
Test with the local file and with the URL, then you will see pq I said that the business weighs a lot.
– user013314