Search code examples
phpxmlsimplexmlcdata

Mixing childs with cdata under the same node. Is it valid?


I need to parse the following xml document (which is coming from external web service):

...
<dati>
    <Riconoscimento>
        <IdentificativoPosizione>xxxx</IdentificativoPosizione>
        <OutputRestituiti>xxx</OutputRestituiti>
    </Riconoscimento>
    <![CDATA[text text text]]>
</dati>    
...

The problem is that until there is node "Riconoscimento" simplexml parser fails to read cdata section, if i remove that child, everything is working without problems.

So the main question is: is it a valid xml document, and if it's valid is there some way to access CDATA section with php without manually removing extra childs?

Thanks in advance.


Solution

  • You can get it like this:

    $x = simplexml_load_string('<root><dati>
        <Riconoscimento>
            <IdentificativoPosizione>xxxx</IdentificativoPosizione>
            <OutputRestituiti>xxx</OutputRestituiti>
        </Riconoscimento>
        <![CDATA[text text text]]>
    </dati></root>', 'SimpleXMLElement', LIBXML_NOCDATA);
    
    var_dump((string)$x->dati);
    

    Note the LIBXML_NOCDATA parameter to convert the CDATA to a text node.