In PHP, processing XML data is one of the common tasks. PHP provides a variety of ways to process XML, among which xml_parse and xml_parser_create are two commonly used functions. These functions provide an event-driven parsing method, so that performance and efficiency can be better guaranteed when processing large amounts of XML data.
In this article, we will explain how to use xml_parse and xml_parser_create to create a custom XML parser and correctly process the XML data encountered during parsing.
xml_parser_create : This function is used to create an XML parser and return a parser resource. Through this parser, PHP can gradually parse data in files or strings according to the XML structure.
xml_parse : This function is used to process parsers created by xml_parser_create and parse incoming XML data. It can parse XML content step by step and trigger related events (for example, when a tag starts, ends, or text nodes are encountered).
xml_parser_create([encoding]);
xml_parse(parser, data[, terminate]);
xml_parser_create When creating a parser, you can choose to specify the encoding (for example: UTF-8 , ISO-8859-1 , etc.).
xml_parse is used to parse data. Each time it parses, it processes an XML data block and processes it through a callback function.
In PHP, we can use xml_parser_create to create a custom parser and handle different events during the parsing process by setting different callback functions. For example, we can set up a callback function to handle the start tag, end tag, text content, etc.
<?php
// Custom callback function
function startElementHandler($parser, $name, $attrs) {
echo "Start tag: $name\n";
// Print label properties
if (!empty($attrs)) {
echo "Tag properties: " . print_r($attrs, true) . "\n";
}
}
function endElementHandler($parser, $name) {
echo "End tag: $name\n";
}
function characterDataHandler($parser, $data) {
echo "Text data: $data\n";
}
// create XML Parser
$parser = xml_parser_create('UTF-8');
// Set callback function
xml_set_element_handler($parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($parser, "characterDataHandler");
// Example XML data
$xml_data = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">PHP for Beginners</title>
<author>John Doe</author>
<price>29.95</price>
</book>
<book>
<title lang="es">PHP para Principiantes</title>
<author>Juan Pérez</author>
<price>25.95</price>
</book>
</bookstore>';
// Analysis XML data
if (!xml_parse($parser, $xml_data)) {
echo "XML Analysis错误: " . xml_error_string(xml_get_error_code($parser)) . "\n";
} else {
echo "XML dataAnalysis成功\n";
}
// 释放Parser资源
xml_parser_free($parser);
?>
xml_set_element_handler sets the callback functions for the start tag and the end tag, namely startElementHandler and endElementHandler respectively.
xml_set_character_data_handler sets the callback function characterDataHandler that handles text data.
When parsing XML data, the callback function is triggered during parsing, handling different XML events.
Start tag: bookstore
Start tag: book
Start tag: title
Tag properties: Array
(
[lang] => en
)
Text data: PHP for Beginners
End tag: title
Start tag: author
Text data: John Doe
End tag: author
Start tag: price
Text data: 29.95
End tag: price
End tag: book
Start tag: book
Start tag: title
Tag properties: Array
(
[lang] => es
)
Text data: PHP para Principiantes
End tag: title
Start tag: author
Text data: Juan Pérez
End tag: author
Start tag: price
Text data: 25.95
End tag: price
End tag: book
End tag: bookstore
XML dataAnalysis成功
When processing XML data, it is sometimes necessary to parse data containing URLs. During parsing, you may encounter XML data similar to the following:
<links>
<link>http://m66.net/page1</link>
<link>http://m66.net/page2</link>
</links>
If we want to get and process these URLs during parsing, we can extract and manipulate these links through a custom callback function.
<?php
// Custom callback function
function startElementHandler($parser, $name, $attrs) {
echo "Start tag: $name\n";
}
function endElementHandler($parser, $name) {
echo "End tag: $name\n";
}
function characterDataHandler($parser, $data) {
// Check if it is URL
if (filter_var($data, FILTER_VALIDATE_URL)) {
echo "Effective URL: $data\n";
} else {
echo "Text data: $data\n";
}
}
// create XML Parser
$parser = xml_parser_create('UTF-8');
// Set callback function
xml_set_element_handler($parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($parser, "characterDataHandler");
// Example XML data
$xml_data = '<?xml version="1.0" encoding="UTF-8"?>
<links>
<link>http://m66.net/page1</link>
<link>http://m66.net/page2</link>
</links>';
// Analysis XML data
if (!xml_parse($parser, $xml_data)) {
echo "XML Analysis错误: " . xml_error_string(xml_get_error_code($parser)) . "\n";
} else {
echo "XML dataAnalysis成功\n";
}
// 释放Parser资源
xml_parser_free($parser);
?>
Start tag: links
Start tag: link
Effective URL: http://m66.net/page1
End tag: link
Start tag: link
Effective URL: http://m66.net/page2
End tag: link
End tag: links
XML dataAnalysis成功
With xml_parser_create and xml_parse , we can parse XML data very flexibly and customize callback functions to handle various events during parsing. Combining these functions, we can process various data in XML according to actual needs, including URLs, tag attributes, text data, etc.
By using custom callback functions, we can process different XML elements and text, making the parsing process more operable and flexible. This event-driven parsing method is especially suitable for application scenarios that require processing large-scale XML data.
Hopefully this tutorial will help you better understand how to use xml_parse and xml_parser_create in PHP to create custom XML parsers and process XML data.