XMLReader 不读取 cdata

XMLReader not reading 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) {
  }
}

这只是取决于您如何打印出该值。如果您将 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;