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

Recommended Answers

All 8 Replies

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.

    // 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"?>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <description>An in-depth look at creating applications
      with XML.</description>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>

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.