-1

I am trying to print out the xml child "pizza" on a php page. Though it is not working, here is my code for parsing the xml file.

<? 

  $dom = simplexml_load_file("menu.xml");

  foreach ($dom->menu->categories->pizzas->pizza as $pizza)
  {
      echo $pizza;
  }


?>

This is the xml file

 <menu>
 <categories>
 <pizzas> 
       <pizza>Cheese Pizza</pizza>
   <pizza>Beef PIzza</pizza>
   <pizza>Chickens Wings Pizza</pizza>
 </pizzas>
 </categories>
 </menu>

I would just like to print out the different kinds of pizzas on my php page. I would like to display a menu on the page.

I get an error of "Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/home.php on line 15"

Please Help! Thanks

Can't see me
  • 501
  • 1
  • 12
  • 23

2 Answers2

1

Remove the top level element menu from that loop and use

foreach ($dom->categories->pizzas->pizza as $pizza)
{
    echo $pizza;
}

If you do print_r($dom); you will get a proof of that structure.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
-1
<?php 
    $dom = simplexml_load_file("menu.xml");
    foreach($dom->categories->pizzas->pizza as $pizza) {
        echo $pizza;
    } // Try This
?>
Mathias
  • 5,642
  • 2
  • 31
  • 46
Neeraj
  • 197
  • 7