开发者

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

开发者 https://www.devze.com 2022-12-11 10:17 出处:网络
I need to parse the following xml document (which is coming from ext开发者_StackOverflow社区ernal web service):

I need to parse the following xml document (which is coming from ext开发者_StackOverflow社区ernal 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.


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.


First of all: this is a valid XML document (see here).

Definition: CDATA sections may occur anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup. CDATA sections begin with the string " <![CDATA[ " and end with the string " ]]> ":

In your case the <data/>-element is a mixed-content element.

$xmlString = <<<XML
<dati>
    <Riconoscimento>
        <IdentificativoPosizione>xxxx</IdentificativoPosizione>
        <OutputRestituiti>xxx</OutputRestituiti>
    </Riconoscimento>
    <![CDATA[text text text]]>
</dati>
XML;
$xml = simplexml_load_string($xmlString);
var_dump((string)$xml);

/*
 * outputs:
 * string(37) "
 *
 *        text text text
 *    "
 */

(there is no need to pass LIBXML_NOCDATA)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号