PHP XML
Working with XML data in PHP applications
📄 What is XML?
XML (eXtensible Markup Language) is a markup language for storing and transporting data. PHP provides multiple ways to read, parse, and manipulate XML documents efficiently.
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>PHP Guide</title>
<author>John Doe</author>
</book>
XML Features
Self-Descriptive
Tags describe the data content
<name>John</name>
Hierarchical
Tree structure with parent-child
<parent><child/></parent>
Platform Independent
Works across all systems
<data>Universal</data>
Data Storage
Store and transport information
<item id="1">Value</item>
🔹 XML Structure Basics
XML documents have a tree structure with a root element containing child elements. Every element must have a closing tag:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="1">
<title>Learning PHP</title>
<author>John Smith</author>
<year>2024</year>
<price>29.99</price>
</book>
<book id="2">
<title>XML Basics</title>
<author>Jane Doe</author>
<year>2023</year>
<price>24.99</price>
</book>
</library>
XML Rules:
- Must have a root element
- Tags are case-sensitive
- All tags must be properly closed
- Elements must be properly nested
🔹 Reading XML in PHP
PHP provides several methods to read XML files. Here's a simple example using file_get_contents:
<?php
// Sample XML file content
$xmlString = '<?xml version="1.0"?>
<books>
<book>
<title>PHP Basics</title>
<author>John Doe</author>
</book>
</books>';
// Load XML from string
$xml = simplexml_load_string($xmlString);
// Access elements
echo "Title: " . $xml->book->title . "<br>";
echo "Author: " . $xml->book->author;
?>
Output:
Title: PHP Basics
Author: John Doe
🔹 Creating XML in PHP
You can create XML documents dynamically using PHP. Here's how to generate XML from PHP data:
<?php
// Create XML document
$xml = new SimpleXMLElement('<users></users>');
// Add user data
$user1 = $xml->addChild('user');
$user1->addChild('name', 'Alice');
$user1->addChild('email', '[email protected]');
$user1->addChild('age', '25');
$user2 = $xml->addChild('user');
$user2->addChild('name', 'Bob');
$user2->addChild('email', '[email protected]');
$user2->addChild('age', '30');
// Format output
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
// Display XML
echo htmlspecialchars($dom->saveXML());
?>
Output:
<?xml version="1.0"?>
<users>
<user>
<name>Alice</name>
<email>[email protected]</email>
<age>25</age>
</user>
<user>
<name>Bob</name>
<email>[email protected]</email>
<age>30</age>
</user>
</users>
🔹 XML Attributes
XML elements can have attributes that provide additional information about the element:
<?php
$xmlString = '<?xml version="1.0"?>
<products>
<product id="101" category="electronics">
<name>Laptop</name>
<price currency="USD">999</price>
</product>
</products>';
$xml = simplexml_load_string($xmlString);
// Access attributes
echo "Product ID: " . $xml->product['id'] . "<br>";
echo "Category: " . $xml->product['category'] . "<br>";
echo "Name: " . $xml->product->name . "<br>";
echo "Price: " . $xml->product->price . " ";
echo $xml->product->price['currency'];
?>
Output:
Product ID: 101
Category: electronics
Name: Laptop
Price: 999 USD
🔹 PHP XML Parsers
PHP offers multiple XML parsing methods, each suited for different needs:
Available Parsers:
- SimpleXML: Easy to use, best for simple XML documents
- DOM Parser: Full control, good for complex XML manipulation
- XMLReader: Memory efficient, ideal for large XML files
- XML Expat Parser: Event-based, fast for streaming XML
🔹 Common XML Use Cases
When to Use XML:
- Configuration files: Store application settings
- Data exchange: Transfer data between systems
- Web services: SOAP and REST API responses
- RSS feeds: Syndicate content
- Document storage: Structured data storage