jrw4 Posted October 4, 2009 Share Posted October 4, 2009 Ok, to my question. I am making a simple atom feed parser. So far here is my code: <?php // read in an xml file $myFile = "an_atom_feed.xml"; $feed = simplexml_load_file($myFile); $xml = $feed->children('http://www.w3.org/2005/Atom'); echo "<h1>".$xml->title . "</h1>\n"; foreach ($xml->entry as $entries) { $child = $entries->children('http://www.w3.org/2005/Atom'); // post title echo "<h3>".$child->title . "</h3>\n"; // post author if (!empty($child->author->name)) echo "<p>Posted by " . $child->author->name . "</p>\n"; // post content if (!empty($child->summary)) echo htmlspecialchars($child->summary) . "<br />\n"; echo $child->content->asXML(); } ?> So far it seems to work fairly well. Obviously I am not dealing with every item in <entry> just the content and title. Now my question comes with, say my feed has the following items in it: <entry> <title>Test entry #3</title> <id>http://example.org/article3</id> <updated>2006-10-03T13:14:34Z</updated> <link href="http://example.org/article3" /> <content type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="/img/smiley" alt=":-)" /></xhtml:div> </content> </entry> <entry> <title>Test entry #4</title> <id>http://example.org/article4</id> <updated>2006-10-03T14:14:34Z</updated> <link href="http://example.org/article4" /> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"><p>This is also <em>very</em> exciting.</p> <p>Yes, it is.</p></div> </content> </entry> I haven't been able to find a tutorial on how to deal with content types like those so I am not sure how to. If you know of tutorials that show me how to deal with stuff like this, that would be very appreciated. If you also know how to deal with it, teaching me would make me très appreciative. Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/ Share on other sites More sharing options...
ngreenwood6 Posted October 4, 2009 Share Posted October 4, 2009 Im not sure what exactly you are trying to do but if you want to display the content one you can display it using the regular $xml->content or wherever you are calling it from. if you need to see a detailed list of what is being show you can always do print_r(). Example: $xml = simplexml_load_file($yourfile); print_r($xml); Then you can see what values are there to use. If you cant see the full page because its ouputting the data you can always view source for the page. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-929997 Share on other sites More sharing options...
jrw4 Posted October 4, 2009 Author Share Posted October 4, 2009 What am I trying to do? I am trying to display each content value for each entry in an atom feed. echoing $xml->entry->content is fine and dandy if the attribute type of the content is "text" or "html". If it is xhtml then I am running into a problem. If I have an item like this in the feed: <entry> <title>Test entry #3</title> <id>http://example.org/article3</id> <updated>2006-10-03T13:14:34Z</updated> <link href="http://example.org/article3" /> <content type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="/img/smiley" alt=":-)" /></xhtml:div> </content> </entry> and I echo $xml->entry->content or $xml->entry->content->asXML() it will not appear properly. Currently I am getting something like: <content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml"> <xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="/img/smiley" alt=":-)"/></xhtml:div> </content> I am trying to figure out how to get it to appear as: <div>This is also <em>very</em> exciting. & <img alt=":-)" src="/img/smiley"/></div> There must be an built in way in SimpleXML to do this instead of having me to do it by hand. This is what I am asking to do. Is how to get an output that looks like the later instead of what I currently have which is the former. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-930003 Share on other sites More sharing options...
jrw4 Posted October 4, 2009 Author Share Posted October 4, 2009 If I try just echoing $child->content; nothing gets outputted for those tags. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-930233 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 SimpleXML does not work well with XML namespaces. I've run into problems when generating xml feed, but I suppose it might alse have problems when parsing XML. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-930238 Share on other sites More sharing options...
jrw4 Posted October 4, 2009 Author Share Posted October 4, 2009 Alright thanks. So it looks like I will have to hand parse the content field then. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-930242 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 You've still got XMLReader which AFAIR has better support for namespaces. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-930274 Share on other sites More sharing options...
Xeoncross Posted August 7, 2012 Share Posted August 7, 2012 I know this topic is now old enough to be legend, but @jrw4 did you ever got anywhere with switching to XMLReader? It seems like libxml's DOM and SimpleXML should be able to handle this. $dom = new DOMDocument('1.0', 'utf-8'); $element = $dom->importNode(dom_import_simplexml($xml->node->here), true); $dom->appendChild($element); return $dom->saveHTML(); Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-1367658 Share on other sites More sharing options...
Mahngiel Posted August 8, 2012 Share Posted August 8, 2012 jrw4 last visited these forums in december of 2009. i don't think he'll be responding to your help. Quote Link to comment https://forums.phpfreaks.com/topic/176431-simplexml-and-atom/#findComment-1367661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.