Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

PHP XML Expat

PHP

PHP XML Expat (XML Parser Functions)

PHP का Expat-based XML Parser low-level, event-driven parsing प्रदान करता है। यह large XML files के लिए तेज़ और memory-efficient होता है क्योंकि यह पूरा DOM memory में नहीं बनाता — बल्कि XML पढ़ते समय events (start tag, end tag, character data) generate होते हैं जिन्हें handler functions से handle किया जाता है।

  • Expat parser functions PHP के लिए event-based parsing provide करते हैं (start element, end element, character data)।
  • यह low-level है — बेहतर performance और कम memory footprint के लिए उपयुक्त।
  • मुख्य functions: xml_parser_create(), xml_set_element_handler(), xml_set_character_data_handler(), xml_parse(), xml_parser_free()

Basic Flow (कदम)

  1. Parser बनाएं: xml_parser_create()
  2. Handlers सेट करें: start, end, character data
  3. XML data पास करें: xml_parse() (string/file chunk-wise)
  4. Parser free करें: xml_parser_free()

Simple Example – Parse XML String

नीचे एक सिंपल example है जो start tag, end tag और character data को print करता है।

उदाहरण: Basic Expat Parser (String)
<?php
function startElement($parser, $name, $attrs) {
   echo "Start: " . $name . "<br>";
}

function endElement($parser, $name) {
   echo "End: " . $name . "<br>";
}

function charData($parser, $data) {
   $text = trim($data);
   if (strlen($text) > 0) {
     echo "Data: " . $text . "<br>";
   }
}

$xml = "<items><item>Laptop</item><item>Phone</item></items>";

$parser = xml_parser_create();
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "charData");

xml_parse($parser, $xml);
xml_parser_free($parser);

?>
Output
Start: ITEMS
Start: ITEM
Data: Laptop
End: ITEM
Start: ITEM
Data: Phone
End: ITEM
End: ITEMS

Parsing Large XML Files (Chunk-wise)

Expat parser की खासियत है कि आप file को छोटे-छोटे chunks में read करके parse कर सकते हैं — जिससे memory कम लगे।

उदाहरण: Parse XML file in chunks
<?php
$parser = xml_parser_create();
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "charData");

$fp = fopen("large.xml", "r");
while (! feof($fp)) {
   $data = fread($fp, 4096);
   if (! xml_parse($parser, $data, feof($fp))) {
     die("XML Error: " . xml_error_string(xml_get_error_code($parser)) . " at line " . xml_get_current_line_number($parser));
   }
}
xml_parser_free($parser);
?>

Error Handling

XML parsing में error handling बहुत जरूरी है—Expat parser कुछ उपयोगी functions देता है:

  • xml_get_error_code($parser) – last error code लौटाता है।
  • xml_error_string($code) – error code का readable message देता है।
  • xml_get_current_line_number($parser) – किस line पर error है यह बताता है।
  • xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0) – tag name के case folding को control करता है।

Attributes Access (Example)

Start element handler के अंदर attributes associative array के रूप में मिलते हैं — इन्हें use कर सकते हैं।

उदाहरण: Attribute Access in start handler
<?php
function startElement($parser, $name, $attrs) {
   if (isset($attrs['id'])) {
     echo "Element " . $name . " with id = " . $attrs['id'] . "<br>";
   }
}
?>

जब Expat उपयोग करें — Pros & Cons

Pros:

  • Memory efficient — बड़े XML files के लिए अच्छा।
  • Fast — event-driven parsing तेज़ होता है।
  • Flexible — आप custom handlers से बहुत control ले सकते हैं।

Cons:

  • Low-level API — DOM या SimpleXML जितना आसान नहीं।
  • State manage करने के लिए आपको खुद structures बनानी पड़ती हैं (stack, current node आदि)।
Article By: Brajlal Prasad
Created on: 03 Nov 2025  11  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store