Parsing XML in PHP
Introduction
XML (eXtensible Markup Language) is a popular format for data representation and exchange. In PHP, there are multiple ways to parse XML data. This tutorial will cover the fundamentals of parsing XML in PHP, including using the SimpleXML extension, the DOM extension, and the XML Parser functions. We'll provide examples for each method to help you understand how to work with XML data effectively.
Using SimpleXML
SimpleXML is an easy-to-use extension for parsing XML data. It converts XML data into an object representation, which can be easily manipulated in PHP.
Example XML
<?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>1984</title> <author>George Orwell</author> <year>1949</year> </book> </books>
Parsing XML with SimpleXML
<?php $xmlString = '<?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>1984</title> <author>George Orwell</author> <year>1949</year> </book> </books>'; $xml = simplexml_load_string($xmlString); foreach ($xml->book as $book) { echo 'Title: ' . $book->title . '<br>'; echo 'Author: ' . $book->author . '<br>'; echo 'Year: ' . $book->year . '<br><br>'; } ?>
Output
Author: F. Scott Fitzgerald
Year: 1925
Title: 1984
Author: George Orwell
Year: 1949
Using the DOM Extension
The DOM extension in PHP provides a more powerful and flexible way to work with XML documents. It allows for the creation, manipulation, and traversal of XML documents using a tree-based structure.
Parsing XML with DOM
<?php $xmlString = '<?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>1984</title> <author>George Orwell</author> <year>1949</year> </book> </books>'; $dom = new DOMDocument(); $dom->loadXML($xmlString); $books = $dom->getElementsByTagName('book'); foreach ($books as $book) { $title = $book->getElementsByTagName('title')->item(0)->nodeValue; $author = $book->getElementsByTagName('author')->item(0)->nodeValue; $year = $book->getElementsByTagName('year')->item(0)->nodeValue; echo 'Title: ' . $title . '<br>'; echo 'Author: ' . $author . '<br>'; echo 'Year: ' . $year . '<br><br>'; } ?>
Output
Author: F. Scott Fitzgerald
Year: 1925
Title: 1984
Author: George Orwell
Year: 1949
Using XML Parser Functions
The XML Parser functions provide an event-based approach to parsing XML. These functions are more complex and less commonly used than SimpleXML and DOM, but they offer fine-grained control over the parsing process.
Parsing XML with XML Parser Functions
<?php function startElement($parser, $name, $attrs) { echo '<b>' . $name . '</b>: '; } function endElement($parser, $name) { echo '<br>'; } function characterData($parser, $data) { echo $data; } $xmlString = '<?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>1984</title> <author>George Orwell</author> <year>1949</year> </book> </books>'; $parser = xml_parser_create(); xml_set_element_handler($parser, "startElement", "endElement"); xml_set_character_data_handler($parser, "characterData"); if (!xml_parse($parser, $xmlString)) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } xml_parser_free($parser); ?>
Output
TITLE: The Great Gatsby
AUTHOR: F. Scott Fitzgerald
YEAR: 1925
BOOK:
TITLE: 1984
AUTHOR: George Orwell
YEAR: 1949