XMLReader没有读取cdata

时间:2017-11-26 08:28:14

标签: php xml nsxmlparser xmlreader cdata

我有问题。我写了这段代码,但我看不懂<![CDATA[Epsilon Yayınları]]>。带有cdata的项目,当我得到它们时它是空的。有替代解决方案吗?

XML:

<urunler>
  <urun>
    <stok_kod>9789753314930</stok_kod>
    <urun_ad><![CDATA[Kırmızı Erik]]></urun_ad>
    <Barkod>9789753314930</Barkod>
    <marka><![CDATA[Epsilon Yayınları]]></marka>
    <Kdv>8,00</Kdv>
    <satis_fiyat>9,5000</satis_fiyat>
    <kat_yolu><![CDATA[Edebiyat>Hikaye]]></kat_yolu>
    <resim>http://basaridagitim.com/images/product/9789753314930.jpg</resim>
    <Yazar>Tülay Ferah</Yazar>
    <Bakiye>2,00000000</Bakiye>
    <detay><![CDATA[]]></detay>
  </urun>
</urunler>

$xml = new XMLReader;
    $xml->open(DIR_DOWNLOAD . 'xml/'.$xml_info['xml_file_name']);
    $doc = new DOMDocument;

    $product_data = array();

    $i=0;


    while ($xml->read() && $xml->name !== 'urun');
    while ($xml->name === 'urun') {  $i++;

        $node = simplexml_import_dom($doc->importNode($xml->expand(), true));


        var_dump($node->urun_ad); die();

转储打印:

object(SimpleXMLElement)#143 (1) {
  [0]=>
  object(SimpleXMLElement)#145 (0) {
  }
}

1 个答案:

答案 0 :(得分:0)

它归结为你如何打印出价值。如果您将var_dump更改为以下任一项,您将获得所需的内容......

//var_dump($node->urun_ad)
echo $node->urun_ad.PHP_EOL;
echo $node->urun_ad->asXML().PHP_EOL;

...输出

Kırmızı Erik
<urun_ad><![CDATA[Kırmızı Erik]]></urun_ad>

需要注意的一点是,如果要在另一种方法中使用该值,则可能必须将其强制转换为字符串(echo会自动执行此操作)。所以第一个是(例如)......

$urun_ad = (string)$node->urun_ad;