--- id: php-xml-parser-expat title: "PHP XML Parser Expat" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Expat parser", "event-based XML", "PHP XML Expat νŒŒμ„œ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "xml", "expat"] raw_sources: ["https://www.w3schools.com/php/php_xml_parser_expat.asp"] applied_in: [] github_commit: "" --- # [[PHP XML Parser Expat]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The Expat parser reduces even a simple tag like `Jani` into exactly THREE distinct events β€” start element, character data, end element β€” and each event type is routed to its own separate handler function, meaning parsing an XML document is really about writing three small callback functions rather than writing traversal logic. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **XML Expat Parser** β€” event-based, non-validating (ignores DTDs), fast, low-memory; part of PHP core. [S1] - **Three-event model** β€” any tag decomposes into: start element, CDATA/character data, end element. [S1] - **`xml_parser_create()`** β€” initializes the parser. [S1] - **`xml_set_element_handler($parser, "start", "stop")`** β€” registers separate functions for opening and closing tags. [S1] - **`xml_set_character_data_handler($parser, "char")`** β€” registers the function for text content between tags. [S1] - **`xml_parse($parser, $data, $isFinal)`** β€” feeds a chunk of data to the parser; `feof($stream)` signals whether this is the final chunk. [S1] - **`xml_error_string()` / `xml_parser_free()`** β€” error reporting and resource cleanup. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Handler functions: `function start($parser, $element_name, $element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --
"; break; case "TO": echo "To: "; break; ... } } function stop($parser, $element_name) { echo "
"; } function char($parser, $data) { echo $data; }`. [S1] - Full parse pipeline: `$stream = fopen("note.xml", "r"); $parser = xml_parser_create(); xml_set_element_handler($parser, "start", "stop"); xml_set_character_data_handler($parser, "char"); while ($data = fread($stream, 4096)) { xml_parse($parser, $data, feof($stream)) or die(...); } xml_parser_free($parser); fclose($stream);`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” note.xml을 이벀트 ν•Έλ“€λŸ¬λ‘œ νŒŒμ‹±ν•΄ "To: Tove" ν˜•νƒœλ‘œ 좜λ ₯ν•˜λŠ” 것이 이벀트 기반 νŒŒμ‹±μ˜ λŒ€ν‘œ μ‹€μ „ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Full Expat parsing pipeline with handler functions (PHP): ```php $stream = fopen("note.xml", "r"); $parser = xml_parser_create(); xml_set_element_handler($parser, "start", "stop"); xml_set_character_data_handler($parser, "char"); while ($data = fread($stream, 4096)) { xml_parse($parser, $data, feof($stream)) or die("XML Error"); } xml_parser_free($parser); fclose($stream); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP XML SimpleXML Get]], [[PHP XML DOM]] - **μ°Έμ‘° λ§₯락:** 이벀트 기반 νŒŒμ‹± β€” 트리 기반 DOM νŒŒμ„œ 챕터와 λŒ€λΉ„λ¨. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP XML Expat Parser β€” https://www.w3schools.com/php/php_xml_parser_expat.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP XML Expat Parser" page (Astra wiki-curation, P-Reinforce v3.1 format).