Re¢ently

  1. More SimpleXML to display OPML

    jake on 2010.04.22
    at 05:28 pm

    Matt Katz recently sent in an email regarding the code we use to display our OPML lists. He needed a second pair of eyes to go over his new class. Turned out the bug wasn’t in his display function but the once over highlighted some other issues with ours.

    The pages here are now updated but we can’t keep this all to ourselves. Here’s the updated code for traversing an OPML file.

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

    Posted in: Programming · Web

     

    Comments (0)

    Comments have been automatically disabled to curtail spam.

by date

« May 2017 »
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 31
today