In PHP, parsing XML files is a common way to handle data exchange and configuration files. The xml_parse() function is an event-driven XML parsing method provided by PHP. By combining other parser functions, elements and attributes in XML files can be read. This article will take you step by step to understand how to use xml_parse() to parse XML and extract the element content and attribute information.
We first prepare a simple XML example for parsing exercises. Assume that the file is named example.xml , the content is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="101" genre="fiction">
<title>PHP getting Started</title>
<author>Zhang San</author>
</book>
<book id="102" genre="programming">
<title>In-depth understanding PHP</title>
<author>Li Si</author>
</book>
</books>
PHP's XML parsing uses an event-based approach. We need to define several handlers and bind them to the parser.
<?php
$parser = xml_parser_create();
function startElement($parser, $name, $attrs) {
echo "Start Element: $name\n";
if (!empty($attrs)) {
echo "property:\n";
foreach ($attrs as $key => $value) {
echo " - $key = $value\n";
}
}
}
function endElement($parser, $name) {
echo "Ending Element: $name\n";
}
function characterData($parser, $data) {
$data = trim($data);
if (!empty($data)) {
echo "content: $data\n";
}
}
// Bind processing function
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");
Now we read the example.xml file and parse it using a parser:
$fp = fopen("example.xml", "r");
if (!$fp) {
die("Unable to open the file");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($parser, $data, feof($fp))) {
die(sprintf(
"XML mistake: %s In the %d OK",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)
));
}
}
xml_parser_free($parser);
fclose($fp);
?>
After running the script, the terminal will output the start and end of each element, the attributes and values in turn. For example:
Start Element: BOOKS
Start Element: BOOK
property:
- ID = 101
- GENRE = fiction
Start Element: TITLE
content: PHP getting Started
Ending Element: TITLE
Start Element: AUTHOR
content: Zhang San
Ending Element: AUTHOR
Ending Element: BOOK
...
Through the xml_parse() function combined with the processor function, we can freely control how to extract data, and even save the parsed results to the database, or generate the corresponding HTML page, such as:
echo "<a href='https://m66.net/book.php?id=101'>Check《PHP getting Started》</a>";
This method is very practical in customizing the data reading process.
xml_parse() provides a lightweight and fast way to parse XML data. Although it is more underlying than the DOM parser, it is a very suitable choice for situations where performance requirements are high or data structures are known. Mastering event-driven parsing will also help you understand the structure and data processing mechanism of XML files more deeply.
Related Tags:
xml_parse