-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
  • 10
  • 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
  • 66,874
  • 43
  • 94
  • 124
Hanky Panky
  • 45,969
  • 8
  • 69
  • 95
-1
<?php 
    $dom = simplexml_load_file("menu.xml");
    foreach($dom->categories->pizzas->pizza as $pizza) {
        echo $pizza;
    } // Try This
?>
Mathias
  • 5,532
  • 2
  • 30
  • 45
Neeraj
  • 197
  • 7