Advance PHP
Advance PHP
Assignment No. 2
1. Provide a brief introduction to XML and explain its main purpose in web
development.
XML (eXtensible Markup Language) is a structured data format used to store
and transport data. It is platform-independent and self-descriptive, making it
widely used in web development, APIs, and configuration files.
Main Purposes of XML in Web Development:
• Data Exchange: Transfers data between systems, such as between a web
server and a client.
• Configuration Files: Used in applications (e.g., config.xml for software
settings).
• APIs and Web Services: Many APIs use XML for data transmission (e.g.,
SOAP-based APIs).
• Storing Structured Data: Used in databases and document-based storage
systems.
Example of an XML File
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
</book>
<book>
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<year>1960</year>
</book>
</books>
// Load XML
$xml = new DOMDocument();
$xml->loadXML($xmlString);
// Extract data
$titles = $xml->getElementsByTagName("title");
foreach ($titles as $title) {
echo "Book Title: " . $title->nodeValue . "<br>";
}
?>
Output:
Book Title: The Great Gatsby
5. What is the SimpleXML extension in PHP, and how is it used to interact
with XML data?
SimpleXML is a PHP extension that provides an easy way to parse and
manipulate XML documents. It allows direct access to XML elements like an
object.