SimpleXMLReader无法正常工作

时间:2017-05-28 07:46:52

标签: php xml xml-parsing xmlreader

我一直在尝试使用SimpleXMLReader

从以下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>

但我只能检索并回显“num”节点的值。 就像这个结果我得到这样的东西:

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

我改变什么并不重要,例如,我似乎无法获得节点<nome>值。 我正在使用这个软件,因为我正在处理一个非常大的XML文件。

在此处下载XML:http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML

在此处获取SimpleXMLReader:https://github.com/dkrnl/SimpleXMLReader

我的代码如下:

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

0 个答案:

没有答案
相关问题