Jump to content

[SOLVED] How can I get to this SimpleXMLElement object's attributes?


ricklee

Recommended Posts

Hi Everyone,

I was wondering if someone might be able to help me with this.  I'm having trouble getting to the string in this object:

 

object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(4880) "fasdfa...etc."
}

 

$goody->0 doesn't work

$goody->attributes() doesn't work either

 

Here's the code that produced the dump:

$filename = 'test_feeds/feed.xml';
$feed = simplexml_load_file($filename); 

$description = $feed->channel->item->description;
$goody = simplexml_load_string($description, 'SimpleXMLElement', LIBXML_NOCDATA);

print var_dump($goody);

 

$filename = 'test_feeds/feed.xml';
$feed = simplexml_load_file($filename); 

$description = $feed->channel->item->description;
$goody = simplexml_load_string($description, 'SimpleXMLElement', LIBXML_NOCDATA);

print $goody[0];

 

Maybe that works?

$filename = 'test_feeds/feed.xml';
$feed = simplexml_load_file($filename); 

$description = $feed->channel->item->description;
$goody = simplexml_load_string($description, 'SimpleXMLElement', LIBXML_NOCDATA);

print $goody[0];

 

Maybe that works?

Nope, that returns null.

What's the original XML?

 

Hi Barand,

It's an RSS feed that starts like this:

<?xml version="1.0" encoding="iso-8859-1"?><rss version="2.0">
   <channel>
      <title>fasd: fasd</title>
      <link>http://www.fasdf.org</link>
      <description>fasd</description>

              <item>
		         <title>asdfas</title>

		         <link>http://www.fasd.org/fasd-2269852.html</link>
		         <description><![CDATA[<pre>::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::

 

The description is what I want to get.  When I've used SimpleXMLElement objects in the past, they looked like this:

object(SimpleXMLElement)#4 (1) {

  ["@attributes"]=>

  array(28) {

 

With the attributes() method allowing iteration over the contents.  In this case, that method doesn't work.

 

Thanks for any advice.

Rick

Doing a bit experimenting.

 

If description only contains CDATA it is ignored

 

xml file

<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
   <channel>
      <title>fasd: fasd</title>
      <link>http://www.fasdf.org</link>
      <description>fasd</description>

              <item>
		         <title>asdfas</title>
		         <link>http://www.fasd.org/fasd-2269852.html</link>
		         <description><![CDATA[<pre>::the description::</pre>]]></description>
              </item>
   </channel>
</rss>  

 

Process

<?php
$xml = simplexml_load_file('names.xml');
echo '<pre>', print_r($xml, true), '</pre>';
?>

 

Output

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

    [channel] => SimpleXMLElement Object
        (
            [title] => fasd: fasd
            [link] => http://www.fasdf.org
            [description] => fasd
            [item] => SimpleXMLElement Object
                (
                    [title] => asdfas
                    [link] => http://www.fasd.org/fasd-2269852.html
                    [description] => SimpleXMLElement Object
                        (
                        )

                )

        )

)


 

 

BUT, if we add some text to the description before the CDATA (same process code) the CDATA appears

 

xml file

<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
   <channel>
      <title>fasd: fasd</title>
      <link>http://www.fasdf.org</link>
      <description>fasd</description>

              <item>
		         <title>asdfas</title>
		         <link>http://www.fasd.org/fasd-2269852.html</link>
		         <description>XYZ<![CDATA[<pre>::the description::</pre>]]></description>
              </item>
   </channel>
</rss> 

 

output

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

    [channel] => SimpleXMLElement Object
        (
            [title] => fasd: fasd
            [link] => http://www.fasdf.org
            [description] => fasd
            [item] => SimpleXMLElement Object
                (
                    [title] => asdfas
                    [link] => http://www.fasd.org/fasd-2269852.html
                    [description] => XYZ::the description::
                )

        )

)

 

original xm file

<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
   <channel>
      <title>fasd: fasd</title>
      <link>http://www.fasdf.org</link>
      <description>fasd</description>

              <item>
		         <title>asdfas</title>
		         <link>http://www.fasd.org/fasd-2269852.html</link>
		         <description><![CDATA[<pre>::the description::</pre>]]></description>
              </item>
   </channel>
</rss> 

 

The only attribute in there is the rss version attribute

 

<?php
$xml = simplexml_load_file('names.xml');

echo $xml['version'];                // --> 2.0
?>

 

Now for the descriptions

 

<?php
$xml = simplexml_load_file('names.xml');
$result = $xml->xpath("channel/item");

while (list(,$node) = each($result)) {
    echo $node->description,'<br>';
}
?>

Now for the descriptions

 

<?php
$xml = simplexml_load_file('names.xml');
$result = $xml->xpath("channel/item");

while (list(,$node) = each($result)) {
    echo $node->description,'<br>';
}
?>

 

Barand, my dear sir. You are a god among men. Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.