php parsing xml file into ul and li

17 Sep

I have a xml file

<?xml version='1.0'?>

I want to print the output as

  • header
    • blocklogo
    • blocknav
  • main
    • blockyouarehere
    • blockpost
  • footer

So, the code I’ve written in php for this is

function gettree($parent,$root){
	if($parent->count() > 0){
		echo "<li>".$parent->getname()."</li><ul>";}
		else{echo "<ul>";}
		foreach( $parent->children() AS $child ){
		echo "</ul>";
		echo "<li>".$parent."</li>";

if (file_exists('site.xml')) {
    $xml = simplexml_load_file('site.xml');


} else {
    exit('Failed to open site.xml.');

Hoping my code snippets may reduce your burden. Thanks for visiting


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: