Assignment No 2. XML Documents and DOM
Assignment No 2. XML Documents and DOM
<Items>
<Item>
<ItemName>Laptop</ItemName>
<ItemPrice>$800</ItemPrice>
<Quantity>10</Quantity>
</Item>
<Item>
<ItemName>Smartphone</ItemName>
<ItemPrice>$500</ItemPrice>
<Quantity>20</Quantity>
</Item>
<Item>
<ItemName>Tablet</ItemName>
<ItemPrice>$300</ItemPrice>
<Quantity>15</Quantity>
</Item>
<Item>
<ItemName>Headphones</ItemName>
<ItemPrice>$100</ItemPrice>
<Quantity>30</Quantity>
</Item>
<Item>
<ItemName>Smartwatch</ItemName>
<ItemPrice>$150</ItemPrice>
<Quantity>25</Quantity>
</Item>
</Items>
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;
<?php
$items = $xml->createElement("Items");
$xml->appendChild($items);
$itemDetails = [
];
// Create the XML stylesheet processing instruction to link the CSS file
$xml->insertBefore(
$xml->documentElement
);
$item = $xml->createElement("Item");
$items->appendChild($item);
$item->appendChild($itemName);
$item->appendChild($itemPrice);
$item->appendChild($quantity);
$xml->formatOutput = true;
$xml->save("Item.xml");
?>
3) Write a PHP script to generate an XML file in the following format in PHP.
Set B 1. Write PHP script to read above created “book.xml” file into simpleXML object. Display
attributes and elements .(Hint L simple_xml_load_file() function )
<Library>
<Book id="1">
<Title>PHP for Beginners</Title>
<Author>John Doe</Author>
<Year>2020</Year>
</Book>
<Book id="2">
<Author>Jane Smith</Author>
<Year>2019</Year>
</Book>
<Book id="3">
<Author>Mary Johnson</Author>
<Year>2018</Year>
</Book>
</Library>
<?php
$xml = simplexml_load_file('book.xml');
exit;
// Iterate over each book and display its attributes and elements
?>
2. Write a PHP script to read “Movie.xml” file and print all MovieTitle and ActorName of file using
DOMDocument Parser. “Movie.xml” file should contain following information with at least 5 records
with values. MovieInfo MovieNo, MovieTitle, ActorName , ReleaseYear
<Movies>
<MovieInfo>
<MovieNo>1</MovieNo>
<MovieTitle>Inception</MovieTitle>
<ActorName>Leonardo DiCaprio</ActorName>
<ReleaseYear>2010</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>2</MovieNo>
<ActorName>Christian Bale</ActorName>
<ReleaseYear>2008</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>3</MovieNo>
<MovieTitle>Interstellar</MovieTitle>
<ActorName>Matthew McConaughey</ActorName>
<ReleaseYear>2014</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>4</MovieNo>
<MovieTitle>The Matrix</MovieTitle>
<ActorName>Keanu Reeves</ActorName>
<ReleaseYear>1999</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>5</MovieNo>
<MovieTitle>Gladiator</MovieTitle>
<ActorName>Russell Crowe</ActorName>
<ReleaseYear>2000</ReleaseYear>
</MovieInfo>
</Movies>
<?php
$xml->load('Movie.xml'); // Ensure Movie.xml is in the same directory or specify the full path
$movies = $xml->getElementsByTagName('MovieInfo');
// Iterate over each movie and print the MovieTitle and ActorName
$movieTitle = $movie->getElementsByTagName('MovieTitle')->item(0)->nodeValue;
$actorName = $movie->getElementsByTagName('ActorName')->item(0)->nodeValue;
?>