Slip no. 3. Write a PHP Script to read ‘BOOK.xml’ file and print specific content of a file using DOMDocument parser. ‘Book.xml’ file should contain following information with at least 5 records with values.
BookInfo
Book NO, Book Name, Author Name, Price, Year.
[Note: Examiners can change the Book info file to Student info, Teacher info]
XML file : book.xml
<?xml version='1.0' encoding ='UTF-8' ?>
<?xml-stylesheet type="text/css"?>
<bookstore>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
<books category="Cooking">
<book_no>2</book_no>
<book_name>ccc</book_name>
<author_name>aaa</author_name>
<price>200</price>
<year>1950</year>
</books>
<books category="YOGA">
<book_no>3</book_no>
<book_name>ddd</book_name>
<author_name>zzz</author_name>
<price>150</price>
<year>2016</year>
</books>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
</bookstore>
PHP file :
<?php
$doc=new DOMDocument();
$doc->load("book.xml");
$name=$doc->getElementsByTagName("book_name");
$year=$doc->getElementsByTagName("year");
echo "Books Names";
foreach($name as $val)
{
echo "<br>".$val->textContent;
}
echo "<br><br> Year";
foreach($year as $value)
{
echo "<br>".$value->textContent;
}
?>
BookInfo
Book NO, Book Name, Author Name, Price, Year.
[Note: Examiners can change the Book info file to Student info, Teacher info]
XML file : book.xml
<?xml version='1.0' encoding ='UTF-8' ?>
<?xml-stylesheet type="text/css"?>
<bookstore>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
<books category="Cooking">
<book_no>2</book_no>
<book_name>ccc</book_name>
<author_name>aaa</author_name>
<price>200</price>
<year>1950</year>
</books>
<books category="YOGA">
<book_no>3</book_no>
<book_name>ddd</book_name>
<author_name>zzz</author_name>
<price>150</price>
<year>2016</year>
</books>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
</bookstore>
PHP file :
<?php
$doc=new DOMDocument();
$doc->load("book.xml");
$name=$doc->getElementsByTagName("book_name");
$year=$doc->getElementsByTagName("year");
echo "Books Names";
foreach($name as $val)
{
echo "<br>".$val->textContent;
}
echo "<br><br> Year";
foreach($year as $value)
{
echo "<br>".$value->textContent;
}
?>
0 Comments