Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question SimpleXMLReader not working properly

    I've been trying to use SimpleXMLReader

    to retrieve data from the following XML structure:

    Code:
     <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 retrieve and echo the "num" node's value. Like this result I get something like this:

    Code:
    /boutique/produto: 228122907 = 0;
    /boutique/produto: 285823820 = 0;
    /boutique/produto: 285823824 = 0;
    /boutique/produto: 285823825 = 0;
    /boutique/produto: 285823826 = 0;
    /boutique/produto: 285823827 = 0;
    It doesn't matter what I change, I can't seem to get the node <nome>value for example. I'm using this software because I'm dealing with a pretty big XML file.

    Download the XML here:
    Code:
    http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML
    Get SimpleXMLReader here:
    Code:
    https://github.com/dkrnl/SimpleXMLReader
    My code is as follows:

    Code:
    <?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();

  2. #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,526
    Thanks
    1
    Thanked 24 Times in 22 Posts
    Quote Originally Posted by matheusgrafix View Post
    Download the XML here:
    Code:
    http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML
    Broken URL. There's nothing there beyond the XML prolog (<?xml ... ?>)
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, ES-Membrane project (Github Pages site)
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •