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

4

I've used SimpleXMLReader to make parse of a gigantic XML structure, that has more than 25mb.

My code works normally, however, I have two problems:

  • I can not turn Xpath into HTML . Well, I studied it, and I saw 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 inside a PHP string in the HTML code.

  • I can not limit the amount of items displayed. I've already looked at all the code in the simpleXMLreader.php file, but I see no way to limit the amount of items extracted from XML, I was told to use foreach next to Xpath in main code, I tried, but it did not 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.

        
    asked by anonymous 05.06.2017 / 09:46

    2 answers

    5

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

    The key is to use XPATH's position method to filter the records what do 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 over 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
    
        
    08.06.2017 / 01:34
    0

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

        
    15.06.2017 / 23:12