Simple XML Reader does not work properly

Asked

Viewed 173 times

3

I’ve been using the Simplexmlreader

to try to extract data from the following XML structure:

 <boutique>
       <produto num="228122907">
            <id_produto><![CDATA[70427038]]></id_produto>
            <nome><![CDATA[Solução Antirrugas - Kit]]></nome>
            <descricao><![CDATA[A melhor combinação do Pegolift com Vitamina C elevada ao extremo e as pluri funções do Pluri-Active. Experimente estes agentes.]]></descricao>
       </produto>
</boutique>

But I’m only being able to display the value of the nodule "one" only, getting this result:

/boutique/produto: 228122907 = 0;
/boutique/produto: 285823820 = 0;
/boutique/produto: 285823824 = 0;
/boutique/produto: 285823825 = 0;
/boutique/produto: 285823826 = 0;
/boutique/produto: 285823827 = 0;

No matter what I change, I can’t, for example, extract the value from the node <nome>. I am using this software because I am dealing with a very large XML file.

Download the XML file here: http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML

Download the Simplexmlreader software here: https://github.com/dkrnl/SimpleXMLReader

My code is this:

<?php
header ("Content-type: text/html, charset=utf-8;");
require_once dirname(__FILE__). "/simplexmlreader.php";
class ExampleXmlReader1 extends SimpleXMLReader
{
    public function __construct()
    {
        // by node name

        $this->registerCallback("nome", array($this, "callbackPrice"));
        // by xpath<br />
///////////////////// Nesta parte não mexe
        $this->registerCallback("/boutique/produto", array($this, "callbackRest"));
    }
    protected function callbackPrice($reader)
    {
        $xml = $reader->expandSimpleXml();
        $attributes = $xml->attributes();
        $ref = (string) $attributes->{"num"};
        if ($ref) {
            $price = floatval((string)$xml);
            $xpath = $this->currentXpath();
            echo "$xpath: $ref = $price;\n";
        }
        return true;
    }
    protected function callbackRest($reader)
    {
        $xml = $reader->expandSimpleXml();
        $attributes = $xml->attributes();
        $ref = (string) $attributes->{"num"};
        if ($ref) {
            $rest = floatval((string) $xml);
            $xpath = $this->currentXpath();
            echo "$xpath: $ref = $rest;\n";
        }
        return true;
    }
}
echo "<pre>";
$file = dirname(__FILE__) . "/boutique.xml";
$reader = new ExampleXmlReader1;
$reader->open($file);
$reader->parse();
$reader->close();
  • 1

    Try to run a foreach in $xml... from what I understand in the GIT documentation, it is he who returns the nodes in the Simplexmlelement Object format. Has other functions to simplify this process as well.

2 answers

4


From what I understand the first callback is to search by the node name and the second callback by the node path, as you want the value of the "name" nodes, so it would look like this:

<?php
header ("Content-type: text/html, charset=utf-8;");
require_once dirname(__FILE__). "/simplexmlreader.php";
class ExampleXmlReader1 extends SimpleXMLReader
{
    public function __construct()
    {
        // by node name
        $this->registerCallback("nome", array($this, "callbackPrice"));

    }
    protected function callbackPrice($reader)
    {
        $xml = $reader->expandSimpleXml();
        $name = $xml;
        $xpath = $this->currentXpath();
        echo "$xpath: Nome = $name;\n";
        return true;
    }
}
echo "<pre>";
$file = dirname(__FILE__) . "/boutique.xml";
$reader = new ExampleXmlReader1;
$reader->open($file);
$reader->parse();
$reader->close();
  • 2

    Good is when the guy can answer without testing! Great, +1

  • I’m testing, I’ll see if I can extract more than one node.

  • If you want the value of another node just call callback again with the value of the node, for example pro "id_product": $this->registerCallback("id_product", array($this, "callbackPrice"));

  • Thank you all very much and especially to Olavo Holanda, my problem has been solved. I would just like to add that "callbackPrice" needs to be changed as identifier.

  • Example: callbackNome

  • Just 2 more questions here, if possible answer me please: 1 - How do I limit the number of items taken from XML? For example, if you want to remove only 5 products from the catalog. 2 - Ali at $file = dirname(__FILE__) . "/boutique.xml";, i need to use XML URL http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML instead of the local archive.

  • 1 - I believe you will have to modify the Simplexmlreader code and pass some new parameter to limit, I saw no ways to do this without modifying the Simplexmlreader. 2 - On the line where you have $Reader->open($file); exchange $file for the XML URL string. $Reader->open("http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML");

  • I found another problem, I am unable to convert the values to HTML.

Show 3 more comments

2

A very simple solution that returns your XML as a PHP array:

<?php

$xml = '<boutique><produto num="228122907"><id_produto><![CDATA[70427038]]></id_produto><nome><![CDATA[Solução Antirrugas - Kit]]></nome><descricao><![CDATA[A melhor combinação do Pegolift com Vitamina C elevada ao extremo e as pluri funções do Pluri-Active. Experimente estes agentes.]]></descricao></produto></boutique>';
$xml_file_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA),true), true);

print_r($xml_file_data);

Return to code execution: Retorno

Browser other questions tagged

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