views:

26

answers:

1

I have an xml file

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <events date="01-10-2009" color="0x99CC00" selected="true"> 
       <event>
            <title>You can use HTML and CSS</title>
            <description><![CDATA[This is the description ]]></description>
        </event>
    </events>
</xml>

I used xpath and and xquery for parsing the xml.

$xml_str = file_get_contents('xmlfile');
$xml = simplexml_load_string($xml_str);
if(!empty($xml))
{
    $nodes = $xml->xpath('//xml/events');
}

i am getting the title properly, but iam not getting description.How i can get data inside the cdata Thanks in advance

+1  A: 

SimpleXML has a bit of a problem with CDATA, so use:

$xml = simplexml_load_file('xmlfile', 'SimpleXMLElement', LIBXML_NOCDATA);
if(!empty($xml))
{
    $nodes = $xml->xpath('//xml/events');
}
print_r( $nodes );

This will give you:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [date] => 01-10-2009
                    [color] => 0x99CC00
                    [selected] => true
                )

            [event] => SimpleXMLElement Object
                (
                    [title] => You can use HTML and CSS
                    [description] => This is the description 
                )

        )

)
slomojo
Thanks slomojo. It is working fine.
THOmas