See what I did there? It was very punny...

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. &amp; <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

    Hopefully you know the point of the type is to let readers know how to display the contents of the content tag 😉. That said, you can easily do:

    if($child->content['type'] == 'xhtml')
    {
        // echo it as normla html, no escaping needed
        echo $child->content;
    }
    else
    {
        // escape it
        echo htmlspecialchars($child->content); 
    }

    Hope that helps.

      Write a Reply...