Simple XML Reader: I can’t convert nodes to HTML or limit the number of items

Asked

Viewed 124 times

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:

  1. I can’t turn the Xpath in HTML. 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.

  2. 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 the Xpathin 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:;&nbspR$". $preco_normal . "<br/>";
    print "<span>Apenas&nbspR$" . $preco_promocao . "<br/></a></span></div>";
    //print "Em&nbsp" . $parcelas . "x de&nbsp:&nbspR$" . $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.

2 answers

4

The solution can be using simple xml, which already comes with PHP?

The secret is to use the method position XPATH to filter the records you want.

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,
        );
    }

    return $products;
}


$products = parse('boutique.xml', 10, 10);

foreach ($products as $product) {
    echo $product['nome'] . ' ' . $product['preco_promocao'] . "\n";
}
?>

The output of this guy on top of the xml of the url you passed is:

DVD Matrix Reloaded 14.99
DVD - Antes do Amanhecer 19.99
DVD O Gênio da Tesoura 19.99
CD Roberto Carlos - Ao Vivo (1988) 21.99
CD Roberto Carlos - Detalhes - 1971 21.99
CD Fagner Ao Vivo - Vol. 2 17.99
CD Jota Quest - Oxigênio 17.99
Barraca para 3 Pessoas Cherokee 3 - Nautika 469.99
Edredom Solteiro 156x230 100% Plumas de Ganso - Daune 546.99
Edredom Casal 220x240 100% Plumas de Ganso - Daune 649.99
Edredom Queen 240x260 100% Plumas de Ganso - Daune 734.99
CD ROM DOCUMENTS TO GO 1.49

  • But this works well with the giant file?

  • 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.

  • All right, what about the HTML part? This solution of yours is completely new code or is it an implement?

  • 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.

  • Man, I’m not getting it.

  • What can’t you get yet? Where are you stalling?

  • 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?

  • 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?

  • Test with the local file and with the URL, then you will see pq I said that the business weighs a lot.

Show 4 more comments

0

I have already marked the best answer to the question, but it would be really nice if someone could answer my specific question. The boy there gave me an answer that solved my problem, but was using an alternative without Simple XML Reader.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.