0% found this document useful (0 votes)
4 views6 pages

Assignment No 2. XML Documents and DOM

The document provides PHP scripts for creating and manipulating XML files, including generating an 'Item.xml' file with item details, reading a 'book.xml' file to display book attributes, and extracting movie titles and actor names from a 'Movie.xml' file. It includes examples of using SimpleXML and DOMDocument for XML processing. The document also specifies the structure of the XML files and the desired output formatting.

Uploaded by

Pooja Bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Assignment No 2. XML Documents and DOM

The document provides PHP scripts for creating and manipulating XML files, including generating an 'Item.xml' file with item details, reading a 'book.xml' file to display book attributes, and extracting movie titles and actor names from a 'Movie.xml' file. It includes examples of using SimpleXML and DOMDocument for XML processing. The document also specifies the structure of the XML files and the desired output formatting.

Uploaded by

Pooja Bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Set A

: $xml=simplexml_load_file("Employee.xml"); foreach($xml->employee as $emp) { } echo "Employee


No = $emp->empno “.”
”; echo “ Employee Name = $emp->empname “.”
”; echo “Employee Salary = $emp->salary “.”
”; echo “Employee Designation= $emp->designation".”
”; 1) Write a script to create XML file named “Item.xml” ……………. ………………. ………………… Store the
details of 5 Items of different Types

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

<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

// Create a new DOMDocument object

$xml = new DOMDocument("1.0", "UTF-8");

// Create the root <Items> element

$items = $xml->createElement("Items");

$xml->appendChild($items);

// Define the details for 5 items

$itemDetails = [

["ItemName" => "Laptop", "ItemPrice" => "$800", "Quantity" => 10],

["ItemName" => "Smartphone", "ItemPrice" => "$500", "Quantity" => 20],

["ItemName" => "Tablet", "ItemPrice" => "$300", "Quantity" => 15],

["ItemName" => "Headphones", "ItemPrice" => "$100", "Quantity" => 30],

["ItemName" => "Smartwatch", "ItemPrice" => "$150", "Quantity" => 25]

];

// Create the XML stylesheet processing instruction to link the CSS file

$xml->insertBefore(

$xml->createProcessingInstruction('xml-stylesheet', 'type="text/css" href="style.css"'),

$xml->documentElement

);

// Loop through each item and add it to the XML structure

foreach ($itemDetails as $itemData) {

$item = $xml->createElement("Item");
$items->appendChild($item);

// Create ItemName element

$itemName = $xml->createElement("ItemName", $itemData["ItemName"]);

$item->appendChild($itemName);

// Create ItemPrice element

$itemPrice = $xml->createElement("ItemPrice", $itemData["ItemPrice"]);

$item->appendChild($itemPrice);

// Create Quantity element

$quantity = $xml->createElement("Quantity", $itemData["Quantity"]);

$item->appendChild($quantity);

// Format the XML output for readability

$xml->formatOutput = true;

// Save the XML document to a file

$xml->save("Item.xml");

echo "XML file 'Item.xml' has been created successfully!";

?>

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 )

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

<Library>

<Book id="1">
<Title>PHP for Beginners</Title>

<Author>John Doe</Author>

<Year>2020</Year>

</Book>

<Book id="2">

<Title>Advanced PHP Programming</Title>

<Author>Jane Smith</Author>

<Year>2019</Year>

</Book>

<Book id="3">

<Title>Learning XML with PHP</Title>

<Author>Mary Johnson</Author>

<Year>2018</Year>

</Book>

</Library>

<?php

// Load the XML file into a SimpleXMLElement object

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

// Check if the file was loaded successfully

if ($xml === false) {

echo "Failed to load the XML file.";

exit;

// Iterate over each book and display its attributes and elements

foreach ($xml->Book as $book) {

// Display the attributes (e.g., 'id' attribute)

echo "Book ID: " . $book['id'] . "<br>";

// Display the elements (e.g., Title, Author, Year)


echo "Title: " . $book->Title . "<br>";

echo "Author: " . $book->Author . "<br>";

echo "Year: " . $book->Year . "<br><br>";

?>

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

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

<Movies>

<MovieInfo>

<MovieNo>1</MovieNo>

<MovieTitle>Inception</MovieTitle>

<ActorName>Leonardo DiCaprio</ActorName>

<ReleaseYear>2010</ReleaseYear>

</MovieInfo>

<MovieInfo>

<MovieNo>2</MovieNo>

<MovieTitle>The Dark Knight</MovieTitle>

<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

// Load the XML file

$xml = new DOMDocument();

$xml->load('Movie.xml'); // Ensure Movie.xml is in the same directory or specify the full path

// Get all <MovieInfo> nodes

$movies = $xml->getElementsByTagName('MovieInfo');

// Iterate over each movie and print the MovieTitle and ActorName

foreach ($movies as $movie) {

// Get the MovieTitle and ActorName elements

$movieTitle = $movie->getElementsByTagName('MovieTitle')->item(0)->nodeValue;

$actorName = $movie->getElementsByTagName('ActorName')->item(0)->nodeValue;

// Print the movie title and actor name

echo "Movie Title: " . $movieTitle . "<br>";

echo "Actor Name: " . $actorName . "<br><br>";

?>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy