how do we extract data from an xml file and display it in php?

I found a sample XML file by searching. So I wrote a small php code to read it. I hope this example will help you.

<?php
    // Loading the XML file
    $xml = simplexml_load_file("books.xml");

    echo "<h2>".$xml->getName()."</h2><br />";


    foreach($xml->children() as $book)
    {
        echo "BOOK : ".$book->attributes()->id."<br />";
        echo "Author : ".$book->author." <br />";
        echo "Title : ".$book->title." <br />";
        echo "Genre : ".$book->genre." <br />";
        echo "Price : ".$book->price." <br />";
        echo "Publish Date : ".$book->publish_date." <br />";
        echo "Description : ".$book->description." <br />";
        echo "<hr/>";
    }
?>

This is the format of the XML file I found

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>
   </book>
</catalog>

Thanks a LOT!!!

Welcome :)
If you got the answer you wanted, you can mark thread as solved :)

thanks mate.. u saved my a**

Lots of thanks

thanks alot......

Hi you are very good man thank you lot. thank you again

I had no idea till I read this. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.