PHP XML Parsers
Different methods to parse and process XML data
🔧 What are XML Parsers?
XML parsers are tools that read and interpret XML documents. PHP offers multiple parsers including SimpleXML, DOM, XMLReader, and Expat, each designed for different parsing needs and performance requirements.
<?php
// Using SimpleXML parser
$xml = simplexml_load_file("data.xml");
echo $xml->title;
?>
Types of XML Parsers
SimpleXML
Easy and intuitive parser
DOM Parser
Full document tree access
XMLReader
Memory-efficient streaming
Expat Parser
Event-based parsing
🔹 SimpleXML Parser
SimpleXML converts XML into an object that can be processed with normal property selectors. It's the easiest parser for beginners:
<?php
// XML data
$xmlString = '<?xml version="1.0"?>
<library>
<book>
<title>PHP Programming</title>
<author>John Smith</author>
<price>29.99</price>
</book>
</library>';
// Parse XML
$xml = simplexml_load_string($xmlString);
// Access data
echo "Title: " . $xml->book->title . "<br>";
echo "Author: " . $xml->book->author . "<br>";
echo "Price: $" . $xml->book->price;
?>
Output:
Title: PHP Programming
Author: John Smith
Price: $29.99
🔹 DOM Parser
The DOM parser loads the entire XML document into memory as a tree structure, allowing full manipulation of the document:
<?php
$xmlString = '<?xml version="1.0"?>
<students>
<student id="1">
<name>Alice</name>
<grade>A</grade>
</student>
<student id="2">
<name>Bob</name>
<grade>B</grade>
</student>
</students>';
// Create DOM document
$dom = new DOMDocument();
$dom->loadXML($xmlString);
// Get all student elements
$students = $dom->getElementsByTagName('student');
foreach ($students as $student) {
$id = $student->getAttribute('id');
$name = $student->getElementsByTagName('name')->item(0)->nodeValue;
$grade = $student->getElementsByTagName('grade')->item(0)->nodeValue;
echo "Student $id: $name - Grade: $grade<br>";
}
?>
Output:
Student 1: Alice - Grade: A
Student 2: Bob - Grade: B
🔹 XMLReader Parser
XMLReader is a pull parser that reads XML node by node, making it memory-efficient for large files:
<?php
// Create XML file content
$xmlContent = '<?xml version="1.0"?>
<products>
<product>
<name>Laptop</name>
<price>999</price>
</product>
<product>
<name>Mouse</name>
<price>25</price>
</product>
</products>';
// Save to temporary file
$tempFile = 'temp.xml';
file_put_contents($tempFile, $xmlContent);
// Create XMLReader
$reader = new XMLReader();
$reader->open($tempFile);
// Read through XML
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'product') {
$reader->read(); // Move to name
$name = $reader->readString();
$reader->next(); // Skip to price
$reader->read();
$price = $reader->readString();
echo "Product: $name - Price: $$price<br>";
}
}
$reader->close();
unlink($tempFile); // Clean up
?>
Output:
Product: Laptop - Price: $999
Product: Mouse - Price: $25
🔹 Expat Parser (Event-Based)
Expat is an event-based parser that triggers callback functions when it encounters XML elements:
<?php
// XML data
$xmlData = '<?xml version="1.0"?>
<note>
<to>John</to>
<from>Jane</from>
<message>Hello!</message>
</note>';
// Create parser
$parser = xml_parser_create();
// Element handlers
function startElement($parser, $name, $attrs) {
echo "<b>Start: $name</b><br>";
}
function endElement($parser, $name) {
echo "<i>End: $name</i><br>";
}
function characterData($parser, $data) {
$data = trim($data);
if (!empty($data)) {
echo "Data: $data<br>";
}
}
// Set handlers
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");
// Parse XML
xml_parse($parser, $xmlData);
// Free parser
xml_parser_free($parser);
?>
Output:
Start: NOTE
Start: TO
Data: John
End: TO
Start: FROM
Data: Jane
End: FROM
🔹 Choosing the Right Parser
Select a parser based on your specific needs and XML document characteristics:
Parser Comparison:
- SimpleXML: Best for small to medium XML files, easy syntax
- DOM: When you need to modify XML or navigate freely
- XMLReader: For very large XML files (memory efficient)
- Expat: When you need fast, event-driven parsing
🔹 Parser Performance Tips
💡 Best Practices:
- File size: Use XMLReader for files larger than 10MB
- Simplicity: Start with SimpleXML for basic needs
- Modification: Use DOM when you need to edit XML
- Speed: Expat is fastest for read-only operations
- Memory: XMLReader uses constant memory regardless of file size