How to display XML content using php

This is the sample XML structure,

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="loaned.xsl"?>
<catalogue>
    <resource>     
        <book>
            <ID>1</ID>
            <title>Art of living</title>
            <date>10/10/2011</date>
            <retdate>30/10/2011</retdate>
            <SID>Chandresh</SID>
        </book>
        <book>
            <ID>2</ID>
            <title>Experiment with Truth</title>
            <date>1/10/2011</date>
            <retdate>15/10/2011</retdate>
            <SID>Dipender</SID>
        </book>
    </resource>
</catalogue>

Code to display XML using php:
<?php
$doc = new DOMDocument();
$doc->load( 'loaned.xml' );
 
$booklist = $doc->getElementsByTagName( "book" );
//$len = $musiclist->length;
foreach( $booklist as $book )
{

 //$sno = $song ->index[];
  $bookids = $book->getElementsByTagName( "ID" );
  $bookid = $bookids->item(0)->nodeValue;
 
  $booktitles= $book->getElementsByTagName( "title" );
  $booktitle = $booktitles->item(0)->nodeValue;
 
  $dates= $book->getElementsByTagName( "date" );
  $date= $dates->item(0)->nodeValue;
 
  $retdates = $book->getElementsByTagName( "retdate" );
  $retdate = $retdates->item(0)->nodeValue;
 
  $sids = $book->getElementsByTagName( "SID" );
  $sid = $sids->item(0)->nodeValue;
 
  echo "<b>$bookid - $booktitle - $date - $retdate - $sid - <a href=\"delete.php?bid=$bookid\">X</a> - <a href=\"modify.php?bid=$bookid\">Modify</a>\n</b><br>";
}
?>

Comments

Popular posts from this blog

CSS Mouse Cursors and Custom Cursors

Block right click or disable keyboard on web pages

What is PAD file? "Submit your software with PAD file to Download.com"