0

test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/"
>

    <item>
        <title>Hello world!</title>
        <link>http://localhost/wordpress/?p=1</link>
        <category><![CDATA[Uncategorized]]></category>
        <HEADLINE><![CDATA[Does Sleep Apnea Offer Some Protection During Heart Attack?]]></HEADLINE>
    </item>
</rss>

i used that code read xml file

    <?php
      if (file_exists('test.xml')) {
        $xml = simplexml_load_file('test.xml');
          echo "<pre>";
          print_r($xml);
          echo "</pre>";
      } else {
         exit('Failed to open test.xml.');
         }
     ?>

Output

      SimpleXMLElement Object
 (
 [@attributes] => Array
    (
        [version] => 2.0
    )

[item] => SimpleXMLElement Object
    (
        [title] => Hello world!
        [link] => http://localhost/wordpress/?p=1
        [category] => SimpleXMLElement Object
            (
            )

        [HEADLINE] => SimpleXMLElement Object
            (
            )

    )

 )

but i have problem with those tag in xml

        <category><![CDATA[Uncategorized]]></category>
    <HEADLINE><![CDATA[Does Sleep Apnea Offer Some Protection During Heart Attack?]]></HEADLINE>

 to read data bcoz of it <![CDATA[]] in category and HEADLINE

i need output

        SimpleXMLElement Object
   (
 [@attributes] => Array
    (
        [version] => 2.0
    )

[item] => SimpleXMLElement Object
    (
        [title] => Hello world!
        [link] => http://localhost/wordpress/?p=1
        [category] => Uncategorized
        [HEADLINE] => Does Sleep Apnea Offer Some Protection During Heart Attack?
    )

 )
thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67

3 Answers3

3

Try this when loading the file

$xml = simplexml_load_file($this->filename,'SimpleXMLElement', LIBXML_NOCDATA);
Svetoslav
  • 4,686
  • 2
  • 28
  • 43
0

Try simply this:

var_dump(
    (string)$xml->item->category,
    (string)$xml->item->HEADLINE
);

It's worth nothing that SimpleXML builds stuff on the fly so print_r() on objects does not necessarily display useful info.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
-1

try casting the

[category] => SimpleXMLElement Object
            (
            )

        [HEADLINE] => SimpleXMLElement Object
            (
            )

to strings, and i think you will get what you need

$item['category']=(string)$item['category'];
Nikola
  • 546
  • 1
  • 6
  • 18