← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 23 of 24

PHP XML Expat Parser

SimpleXML कुछ भी पढ़ने से पहले पूरे XML document को एक object tree की तरह memory में load करता है, जो extremely बड़ी XML files के लिए एक problem बन जाता है। Expat parser पूरी तरह अलग approach लेता है: यह किसी XML document से शुरू से अंत तक एक बार गुज़रता है, हर opening tag, closing tag, और text के piece पर callback functions चलाते हुए।
Syntax
php
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_handler", "end_handler");
xml_parse($parser, $data, true);
xml_parser_free($parser);

एक Streaming Parser क्यों इस्तेमाल करें

SimpleXML और DOM दोनों कुछ भी पढ़ने से पहले पूरे XML document को एक tree की तरह memory में load करते हैं -- छोटी files के लिए ठीक है, लेकिन एक multi-gigabyte XML export के लिए potentially एक serious memory problem।

Expat इसके बजाय document से एक बार streams करता है, इसे टुकड़े-टुकड़े process करता है बिना कभी पूरी चीज़ memory में रखे।

उदाहरण: Why Use a Streaming Parser

php
<?php
// Print "Expat streams through XML piece by piece instead of loading a full tree into memory" to the output
echo "Expat streams through XML piece by piece instead of loading a full tree into memory";
?>

एक Parser बनाना और Handlers Register करना

xml_parser_create() एक नया parser instance बनाता है, और xml_set_element_handler($parser, $startCallback, $endCallback) दो functions register करता है: एक हर बार एक opening tag मिलने पर call होता है, और दूसरा हर बार एक closing tag मिलने पर।

उदाहरण: Creating a Parser and Registering Handlers

php
<?php
// Declare `$parser`, set to `xml_parser_create()`
$parser = xml_parser_create();
xml_set_element_handler($parser, function ($parser, $name) {
    // Print "Start: $name\n" to the output
    echo "Start: $name\n";
}, function ($parser, $name) {
    // Print "End: $name\n" to the output
    echo "End: $name\n";
});
// Print "Handlers registered" to the output
echo "Handlers registered";
// Call `xml_parser_free($parser)`
xml_parser_free($parser);
?>

XML Data Parse करना

xml_parse($parser, $data, $isFinal) XML text का एक chunk parser को देता है, उस chunk को process करते हुए registered callbacks trigger करता है -- final $isFinal argument की तरह true pass करना parser को बताता है कि यह data का last piece है, इसे एक incomplete document detect करने देते हुए।

उदाहरण: Parsing XML Data

php
<?php
// Declare `$parser`, set to `xml_parser_create()`
$parser = xml_parser_create();
xml_set_element_handler($parser, function ($p, $name) { echo "<$name>\n"; }, function ($p, $name) { echo "</$name>\n"; });
// Call `xml_parse($parser, "<book><title>PHP</title></book>", true)`
xml_parse($parser, "<book><title>PHP</title></book>", true);
// Call `xml_parser_free($parser)`
xml_parser_free($parser);
?>

Handler State के साथ Data बनाना

क्योंकि हर Expat callback सिर्फ single tag या text chunk देखता है जो उसे अभी दिया गया, कई calls में meaningful state track करने के लिए (जैसे इस समय कौन सा element open है, या parsed records की एक list accumulate करना) handler functions के बाहर variables इस्तेमाल करना ज़रूरी है, अक्सर global या reference से captured।

उदाहरण: Building Data with Handler State

php
<?php
// Declare `$titles` as an empty array
$titles = [];
// Declare `$parser`, set to `xml_parser_create()`
$parser = xml_parser_create();
// Call `xml_set_element_handler($parser, function () {}, function () {})`
xml_set_element_handler($parser, function () {}, function () {});
xml_set_character_data_handler($parser, function ($p, $data) use (&$titles) {
    if (trim($data) !== '') $titles[] = trim($data);
});
// Call `xml_parse($parser, "<book>PHP Basics</book>", true)`
xml_parse($parser, "<book>PHP Basics</book>", true);
// Print a human-readable dump of `$titles`
print_r($titles);
// Call `xml_parser_free($parser)`
xml_parser_free($parser);
?>

Parser Resources Free करना

xml_parser_free($parser) parsing complete होते ही parser instance के resources release कर देता है -- उन scripts में explicitly call करना ज़रूरी है जो कई parsers बनाती हैं (जैसे एक loop में कई files process करना), हालाँकि PHP script खत्म होने पर automatically इसे clean up भी कर देता है।

उदाहरण: Freeing Parser Resources

php
<?php
// Declare `$parser`, set to `xml_parser_create()`
$parser = xml_parser_create();
// Call `xml_parse($parser, "<book></book>", true)`
xml_parse($parser, "<book></book>", true);
// Call `xml_parser_free($parser)`
xml_parser_free($parser);
// Print "Parser resources released" to the output
echo "Parser resources released";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. छोटे, everyday XML parsing tasks के लिए Expat चुनना जहाँ SimpleXML कहीं simpler और पूरी तरह adequate होगा -- Expat की complexity सिर्फ बहुत बड़ी files के लिए worth है।
  2. यह भूल जाना कि Expat callbacks parsing आगे बढ़ने के साथ document order में fire होते हैं -- आप उस data को "look ahead" नहीं कर सकते जो अभी पहुँची ही नहीं, एक पूरी तरह-loaded SimpleXML tree के उलट।
  3. callback calls के बीच parsing state (जैसे इस समय कौन सा element open है) track न करना, क्योंकि हर callback सिर्फ उस single tag या text chunk को जानता है जो उसे अभी दिया गया।
चैप्टर सारांश
  • xml_parser_create() एक नया Expat parser instance बनाता है, और xml_set_element_handler() opening और closing tags के लिए callback functions register करता है।
  • xml_parse($parser, $data) parser को XML data देता है, जो इसे process करता है और रास्ते में registered callbacks trigger करता है।
  • Expat एक streaming, event-based parser है जो कभी पूरे document को एक साथ memory में नहीं रखता, इसे बहुत बड़ी XML files के लिए well-suited बनाते हुए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.