Php - XML troubleshooting

I have a file, “books.xml” with this:

<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>
   
   <course category = "JAVA">
      <title lang = "en">Java</title>
      <tutor>Gopal</tutor>
      <duration></duration>
      <price>$30</price>
   </course>
   
   <course category = "HADOOP">
      <title lang = "en">Hadoop</title>.
      <tutor>Satish</tutor>
      <duration>3>/duration>
      <price>$50</price>
   </course>
   
   <course category = "HTML">
      <title lang = "en">html</title>
      <tutor>raju</tutor>
      <duration>5</duration>
      <price>$50</price>
   </course>
   
   <course category = "WEB">
      <title lang = "en">Web Technologies</title>
      <tutor>Javed</tutor>
      <duration>10</duration>
      <price>$60</price>
   </course>

</tutorialspoint>

I have a main file with this:

  <?php
     $xml = simplexml_load_file("books.xml") or die("Error: Cannot create object");
     
     foreach($xml->children() as $books) { 
        echo $books->title . "<br> "; 
        echo $books->tutor . "<br> "; 
        echo $books->duration . "<br> ";
        echo $books->price . "<hr>"; 
     }
  ?>
----------------------------- It outputs:

children() as $books) { echo $books->title . “
”; echo $books->tutor . “
”; echo $books->duration . “
”; echo $books->price . “
”; } ?>

can view here: https://iglooo.000webhostapp.com/xml/php.html

What am I doing wrong?

I think this is actually what is written in your code (although, feel free to correct me). Your main file is outputting that because of this line:

foreach($xml->children() as $books) { 

It looks to me like you’re telling it to output the below using $xml->. Is this how you meant to write it?

THE SOLUTION :

1 Like

How on earth did you find that?

https://iglooo.000webhostapp.com/xml/books.xml

1 Like

@freesodas Is your issue solved??