1. Using PHP's SimpleXML to display OPML

    jake on 2007.05.19
    at 06:25 pm

    We like sharing OPML files listing the feeds Brian and I read. Originally we used a simple script that just spit out a long unordered list without much structure. Over time the file it was based on disappeared and the code became muddled. It was time to start fresh.

    After some searching I found the Optimal OPML Browser which looked to be a good replacement with minimal effort. But after it failed to work I discovered that our host does not have all the requirements installed.

    What to do?

    In a recent article Tim Koschuetzki wrote about parsing, the more general, XML with PHP 5’s new SimpleXML. Using SimpleXML and a recursive function I could now sift through the OPML and write out some XHTML compliant code.

    First a recursive function modified from Tim’s simple indented text display.

    function displayChildrenRecursive($xmlObj,$depth=1) {
    	if (count($xmlObj->children()) > 0) { echo str_repeat("\t",$depth).''<ul class="opmlGroup">'."\n"; }
    	foreach($xmlObj->children() as $child) {
    		if (isset($child['htmlUrl'])) {
    			echo str_repeat("t",$depth).'<li><a href="'.htmlentities($child['htmlUrl']).'" title="'.htmlentities($child['description']).'">'.htmlentities($child['text']).'</a> [<a href="'.htmlentities($child['xmlUrl']).'">XML Feed</a>]</li>'."n";
    		} elseif (isset($child['text']) && !isset($child['htmlUrl'])) {
    			echo str_repeat("\t",$depth).'<li><h'.($depth+1).'>'.htmlentities($child['text']).'</h'.($depth+1).'>'."n";
    	if (count($xmlObj->children()) > 0) { 
    		echo str_repeat("\t",$depth).'</ul>'."\n"; 
    		if ($depth > 1) { echo str_repeat("\t",$depth-1).'</li>'."\n"; }

    The first thing you see is an if statement to check for children. When children are found it creates a new unordered list to wrap them in. It then loops through all of said children looking for new possible groups and any actual feeds. Notice the function being called within itself to spit out children’s children. And finally a check again to close out that depth’s list.

    Now that we have the function to parse through the file we need to create a new instance of the SimpleXML object and call the function.

    $opmlFile = new SimpleXMLElement("path to OPML file",null,true);

    One thing you may notice is that instead of referencing the entire object I target only the body with $opmlFile->body. This gives us the list of feeds instead of the entire file.

    Again we have a functional method for listing OPML files. And this more robust method leaves things open for future modifications. Please let me know if you have any questions about the above method.

    Posted in: Programming · Web


    Comments (1)

    1. Tim Koschuetzki said on 2007.05.20 at 02:09 pm

      Nice article

      Great article, Jake. I like the concept of sharing OPML files. Will definately blog about this today.

      Keep up the good work and thanks for referencing my article!


    Comments have been automatically disabled to curtail spam.

by date

« May 2020 »
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30