ricklee Posted April 20, 2007 Share Posted April 20, 2007 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); Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/ Share on other sites More sharing options...
per1os Posted April 20, 2007 Share Posted April 20, 2007 $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? Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234290 Share on other sites More sharing options...
Barand Posted April 20, 2007 Share Posted April 20, 2007 Have you tried $goody['attribute'] Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234319 Share on other sites More sharing options...
ricklee Posted April 21, 2007 Author Share Posted April 21, 2007 $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. Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234568 Share on other sites More sharing options...
ricklee Posted April 21, 2007 Author Share Posted April 21, 2007 Have you tried $goody['attribute'] Hi, Since you suggested it, I tried it, but it returned null. Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234569 Share on other sites More sharing options...
Barand Posted April 21, 2007 Share Posted April 21, 2007 What's the original XML? Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234644 Share on other sites More sharing options...
ricklee Posted April 21, 2007 Author Share Posted April 21, 2007 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 Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234847 Share on other sites More sharing options...
Barand Posted April 21, 2007 Share Posted April 21, 2007 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:: ) ) ) Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234875 Share on other sites More sharing options...
Barand Posted April 21, 2007 Share Posted April 21, 2007 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>'; } ?> Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-234908 Share on other sites More sharing options...
ricklee Posted April 22, 2007 Author Share Posted April 22, 2007 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. Link to comment https://forums.phpfreaks.com/topic/47940-solved-how-can-i-get-to-this-simplexmlelement-objects-attributes/#findComment-235415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.