Wednesday, January 8, 2025

WT-II Assignment-2

 Assignment No 2. XML documents and DOM

 Set A :
1) Write a script to create XML file named “Item.xml”
<Item>
<ItemName> ................ </ItemName>
<ItemPrice> ...................</ItemPrice>
<Quantity> ..................... </Quantity>
</Item>
Store the details of 5 Items of different Types

Solution:-

<?xml version="1.0" encoding ="UTF-8"?>
<Item>

<item type="fruit">
<ItemName>Mango</ItemName>
<ItemPrice>12</ItemPrice>
<Quantity>12</Quantity>
</item>

<item type="Vegetables">
<ItemName>Potato</ItemName>
<ItemPrice>10</ItemPrice>
<Quantity>12</Quantity>
</item>

<item type="Cerial">
<ItemName>Moong</ItemName>
<ItemPrice>120</ItemPrice>
<Quantity>2</Quantity>
</item>

<item type="Grocerry">
<ItemName>Sugar</ItemName>
<ItemPrice>40</ItemPrice>
<Quantity>2</Quantity>
</item>

<item type="Stationary">
<ItemName>Pen</ItemName>
<ItemPrice>4</ItemPrice>
<Quantity>12</Quantity>
</item>
</Item>
 

2) Link “ Item. Xml” file to the CSS style sheet and get well formatted output as given below
i)ItemName :
Color : red;
Font-family : copperplate Gothic Light;
Font-size : 16pt;
Font :bold;

ii)ItemPrice and Quantity
color:yellow;
font-family:Arial;
font-size:12 pt;
font:bold;

 

Solution:-

A2.css

  ItemName
{
    color:red;
    font-family:copperplate Gothoic Light;
    font-size:16pt;
    font:bold;
}
ItemPrice,Quantity
{
    color:yellow;
    font-family:Arial;
    font-size:12pt;
    font:bold;
}


A2.xml

<?xml version="1.0" encoding ="UTF-8"?>
<?xml-stylesheet type = "text/css" href = "A2.css"?>
<Item>
<item type="fruit">
<ItemName>Mango</ItemName>
<ItemPrice>12</ItemPrice>
<Quantity>12</Quantity>
</item>

<item type="Vegetables">
<ItemName>Potato</ItemName>
<ItemPrice>10</ItemPrice>
<Quantity>12</Quantity>
</item>

<item type="Cerial">
<ItemName>Moong</ItemName>
<ItemPrice>120</ItemPrice>
<Quantity>2</Quantity>
</item>

<item type="Grocerry">
<ItemName>Sugar</ItemName>
<ItemPrice>40</ItemPrice>
<Quantity>2</Quantity>
</item>

<item type="Stationary">
<ItemName>Pen</ItemName>
<ItemPrice>4</ItemPrice>
<Quantity>12</Quantity>
</item>
</Item>

 

3) Write a PHP script to generate an XML file in the following format in PHP.
<?xml version="1.0" encoding="UTF-8"?>
<BookInfo>
<book>
<bookno>1</bookno>
<bookname>JAVA</bookname>
<authorname> Balguru Swami</authorname>
<price>250</price>
<year>2006</year>
</book>
<book>
<bookno>2</bookno>
22<bookname>C</bookname>
<authorname> Denis Ritchie</authorname>
<price>500</price>
<year>1971</year>
</book>
</BookInfo>


Solution:-

<!-- You have to just write this program xml file will be automatically created after running this php code.. -->

<?php
$bookInfo = new SimpleXMLElement("<BookInfo/>");
$book=$bookInfo ->addChild("book");
$book->addChild("bookno","1");
$book->addChild("bookname","java");
$book->addChild("authorname","Balguru Swami");
$book->addChild("price","250");
$book->addChild("year","2006");

$book=$bookInfo ->addChild("book");
$book->addChild("bookno","2");
$book->addChild("bookname","C");
$book->addChild("authorname","Denis Ritchie");
$book->addChild("price","500");
$book->addChild("year","1971");

$bookInfo -> asXML("Book.xml");
?>

 Set B
1. Write PHP script to read above created “book.xml” file into simpleXML object. Display attributes and elements .(Hint  simple_xml_load_file() function )

Solution:-

Php file -
<?php
$xml=simplexml_load_file("Book.xml") or die("eror:cannot create object");
var_dump($xml);
?>


2. Write a PHP script to read “Movie.xml” file and print all MovieTitle and ActorName of file using DOM Document Parser. “Movie.xml” file should contain following information with at least 5 records with values.

MovieInfo
MovieNo, MovieTitle, ActorName , ReleaseYear


Solution:-

Movie.xml

<?xml version="1.0" encoding="UTF-8"?>
<MovieInfo>
    <Movie>
        <MovieNo>
            1
        </MovieNo>
        <MovieTitle>
            WAR
        </MovieTitle>
        <ActorName>
            Hritik Roshan
        </ActorName>
        <ReleaseYear>
            2020
        </ReleaseYear>
    </Movie>
    <Movie>
        <MovieNo>
            2
        </MovieNo>
        <MovieTitle>
            DDLJ
        </MovieTitle>
        <ActorName>
            Shahrukh Khan
        </ActorName>
        <ReleaseYear>
            1994
        </ReleaseYear>
    </Movie>
    <Movie>
        <MovieNo>
            3
        </MovieNo>
        <MovieTitle>
            KGF
        </MovieTitle>
        <ActorName>
            Yash
        </ActorName>
        <ReleaseYear>
            2022
        </ReleaseYear>
    </Movie>
    <Movie>
        <MovieNo>
            4
        </MovieNo>
        <MovieTitle>
            RRR
        </MovieTitle>
        <ActorName>
            Ram
        </ActorName>
        <ReleaseYear>
            2022
        </ReleaseYear>
    </Movie>
    <Movie>
        <MovieNo>
            5
        </MovieNo>
        <MovieTitle>
            PUSHPA
        </MovieTitle>
        <ActorName>
            Allu Arjun
        </ActorName>
        <ReleaseYear>
            2022
        </ReleaseYear>
    </Movie>
</MovieInfo>


Movie.php

<?php

$xml = simplexml_load_file('Movie.xml');

echo '<h2>Movie Title and Actor Name</h2>';

$list = $xml->Movie;

for ($i = 0; $i < count($list); $i++) {

    echo 'Movie Name: ' . $list[$i]->MovieTitle . '<br>';

    echo 'Actor Name: ' . $list[$i]->ActorName . '<br><br>';

}


 Set C
1. Create a XML file which gives details of movies available in “Movie CD Store “ from following categories
a) Classical
b) Horror
c) Action
Elements in each category are in the following format
<Category>
<MovieTitle> ................ </ MovieTitle >
<ActorName> ...................</ActorName>
<ReleaseYear> ..................... </ReleaseYear>
</Category>
Save the file with name “movies.xml”

 Solution:-

 catalogue.xml

<?xml version="1.0" ?>
<?xml-stylesheet type="text/css" href="catlogue.css"?>
<CATALOG>
<CD code="B1">
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD code="B2">
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>


Catalogue.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="catlogue.css">
</head>
<body>
<?php
$xml=simplexml_load_file('catlogue.xml')or die("cannot die");
?>
<center>
</b>CD details</b></center>
<table border="1">
<th>CD code</th>
<th>Title</th>
<th>ARTIST</th>
<th>COUNTRY</th>
<th>COMPANY</th>
<th>PRICE</th>
<th>Year</th>
<?php
foreach($xml->CD as $a)
{
echo"<tr><td>".$a['code']."</td>";
echo"<td>".$a->TITLE."</td>";
echo"<td>".$a->ARTIST."</td>";
echo"<td>".$a->COUNTRY."</td>";
echo"<td>".$a->COMPANY."</td>";
echo"<td>".$a->PRICE."</td>";
echo"<td>".$a->YEAR."</td>
</tr>";
}
?>
</table>
</body>
</html>


catlogue.css
table th{color:blue; font-size: 20pt;}
table td{color:green; font-size: 20pt;}

No comments:

Post a Comment