CData in simplexml opened from XMLReader

I have a bunch of XML file that I load into my script using XMLReader, creating a DOM object and then converting to Simplexml.

The problem is that one of the XML files uses CDATA, which SIMPLEXML ignores and usually uses SIMPLEXML_LOAD_FILE. I would add the LIBXML_NOCDATA parameter, but since I am using simplexml_import_dom, I cannot figure out how to ignore the CDATA in the sceanrio below.

Any ideas please?

Thanks a lot Brett

$file = 'test.xml';
$reader = new XMLReader();
$reader->open($file);       
while ($reader->read())
{
    // are we in a product?
    if ($reader->nodeType == XMLReader::ELEMENT &&
        strtolower($reader->localName) == 'product')

    {
        if (!$node = $reader->expand()) {
            //do nothing 
        }
        else {
             // expand the node into a DOMNode
        // Convert to SimpleXML via DOM, messy but SimpleXML is soo much nicer.
        $dom  = new DomDocument();
        $dom->appendChild($dom->importNode($node, true));
        $products = simplexml_import_dom($dom);

        // do whatever we want to do with the product data

}

      

+3


source to share


2 answers


You can try something like:



<?php
$str = $dom->saveXML();
$product = simplexml_load_string($str, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);

      

+4


source


There seems to be a lot of confusion and misinformation about SimpleXML's handling of CDATA nodes. It doesn't ignore CDATA , it just remembers that the particular node was in CDATA by representing it as an object that is not a simple string.

If you are always good at casting SimpleXML values ​​to string values ​​explicitly, you should see the CDATA content plain.



See http://php.net/function.simplexml-load-string.php#84365 for details

Alternatively, the parameter LIBXML_NOCDATA

you specify can be passed to simplexml_load_string

. If you really need XMLReader for some other reason, you can use $reader->readOuterXML()

instead of converting via DOMDocument

.

+1


source







All Articles