{"id":3124,"date":"2025-03-22T10:26:06","date_gmt":"2025-03-22T02:26:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3124"},"modified":"2025-03-22T10:26:06","modified_gmt":"2025-03-22T02:26:06","slug":"php-xml-expat-%e8%a7%a3%e6%9e%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-xml-expat-%e8%a7%a3%e6%9e%90%e5%99%a8\/","title":{"rendered":"PHP XML Expat \u89e3\u6790\u5668"},"content":{"rendered":"\n<p>PHP \u7684 XML Expat \u89e3\u6790\u5668\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684 XML \u89e3\u6790\u5668\u3002\u5b83\u7528\u4e8e\u89e3\u6790 XML \u6587\u6863\u5e76\u5c06\u89e3\u6790\u8fc7\u7a0b\u4e2d\u7684\u6bcf\u4e2a\u4e8b\u4ef6\u4f20\u9012\u7ed9\u4f60\u5b9a\u4e49\u7684\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u79cd\u89e3\u6790\u5668\u901a\u5e38\u7528\u4e8e\u9700\u8981\u9010\u6b65\u89e3\u6790\u548c\u5904\u7406\u5927\u6587\u4ef6\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5f53\u5185\u5b58\u6548\u7387\u975e\u5e38\u91cd\u8981\u65f6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u6982\u8ff0<\/strong><\/h2>\n\n\n\n<p>Expat \u662f\u4e00\u79cd\u6d41\u5f0f XML \u89e3\u6790\u5668\uff0c\u5b83\u4f1a\u9010\u884c\u8bfb\u53d6 XML \u6587\u6863\uff0c\u5e76\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u62a5\u544a\u6bcf\u4e00\u4e2a XML \u5143\u7d20\u3001\u5c5e\u6027\u548c\u5176\u4ed6\u76f8\u5173\u4e8b\u4ef6\u3002Expat \u662f\u4e00\u79cd\u8f83\u4e3a\u8f7b\u91cf\u7684\u89e3\u6790\u5668\uff0c\u5b83\u9002\u7528\u4e8e\u5b9e\u65f6\u89e3\u6790\u5927\u578b XML \u6587\u6863\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u5c06\u6574\u4e2a\u6587\u6863\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u5982\u4f55\u4f7f\u7528 Expat \u89e3\u6790\u5668<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521d\u59cb\u5316\u89e3\u6790\u5668<\/strong><br>\u4f7f\u7528 <code>xml_parser_create()<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Expat \u89e3\u6790\u5668\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<\/strong><br>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406 XML \u6587\u6863\u7684\u5404\u79cd\u4e8b\u4ef6\uff0c\u4f8b\u5982\u5f00\u59cb\u6807\u7b7e\u3001\u7ed3\u675f\u6807\u7b7e\u3001\u5b57\u7b26\u6570\u636e\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u6790 XML \u6570\u636e<\/strong><br>\u4f7f\u7528 <code>xml_parse()<\/code> \u51fd\u6570\u4f20\u9012 XML \u6570\u636e\u3002\u89e3\u6790\u5668\u4f1a\u6839\u636e XML \u6570\u636e\u7684\u5185\u5bb9\u8c03\u7528\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u91ca\u653e\u89e3\u6790\u5668<\/strong><br>\u5b8c\u6210\u89e3\u6790\u540e\uff0c\u9700\u8981\u4f7f\u7528 <code>xml_parser_free()<\/code> \u6765\u91ca\u653e\u89e3\u6790\u5668\u5b9e\u4f8b\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">3.<strong>PHP \u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Expat \u89e3\u6790\u5668\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u56de\u8c03\u51fd\u6570\uff1a\u5f00\u59cb\u6807\u7b7e\nfunction startElement($parser, $name, $attrs) {\n    echo \"Start element: $name\\n\";\n    if (!empty($attrs)) {\n        foreach ($attrs as $key =&gt; $value) {\n            echo \"Attribute: $key = $value\\n\";\n        }\n    }\n}\n\n\/\/ \u56de\u8c03\u51fd\u6570\uff1a\u7ed3\u675f\u6807\u7b7e\nfunction endElement($parser, $name) {\n    echo \"End element: $name\\n\";\n}\n\n\/\/ \u56de\u8c03\u51fd\u6570\uff1a\u5b57\u7b26\u6570\u636e\nfunction characterData($parser, $data) {\n    echo \"Character data: $data\\n\";\n}\n\n\/\/ \u521b\u5efa\u89e3\u6790\u5668\n$parser = xml_parser_create();\n\n\/\/ \u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\nxml_set_element_handler($parser, \"startElement\", \"endElement\");\nxml_set_character_data_handler($parser, \"characterData\");\n\n\/\/ XML \u6570\u636e\n$data = '&lt;book&gt;&lt;title&gt;PHP for Beginners&lt;\/title&gt;&lt;author&gt;John Doe&lt;\/author&gt;&lt;\/book&gt;';\n\n\/\/ \u89e3\u6790 XML \u6570\u636e\nif (!xml_parse($parser, $data, true)) {\n    die(\"XML Parse Error: \" . xml_error_string(xml_get_error_code($parser)));\n}\n\n\/\/ \u91ca\u653e\u89e3\u6790\u5668\nxml_parser_free($parser);\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u529f\u80fd\u89e3\u6790<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>xml_parser_create()<\/code><\/strong><br>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 XML \u89e3\u6790\u5668\u3002\u8fd4\u56de\u4e00\u4e2a\u8d44\u6e90\u7c7b\u578b\u7684\u89e3\u6790\u5668\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong><code>xml_set_element_handler()<\/code><\/strong><br>\u7528\u6765\u8bbe\u7f6e XML \u5143\u7d20\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u7b7e\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong><code>xml_set_character_data_handler()<\/code><\/strong><br>\u7528\u6765\u8bbe\u7f6e\u5b57\u7b26\u6570\u636e\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong><code>xml_parse()<\/code><\/strong><br>\u89e3\u6790 XML \u6570\u636e\u3002\u8be5\u51fd\u6570\u6bcf\u6b21\u5904\u7406\u4e00\u4e2a\u6bb5\u843d\u6216\u4e8b\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong><code>xml_error_string()<\/code><\/strong><br>\u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c<code>xml_error_string()<\/code> \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong><code>xml_parser_free()<\/code><\/strong><br>\u89e3\u6790\u5b8c\u6210\u540e\uff0c\u91ca\u653e\u89e3\u6790\u5668\u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u89e3\u6790\u8fc7\u7a0b\u4e0e\u4e8b\u4ef6<\/strong><\/h2>\n\n\n\n<p>\u5728 Expat \u89e3\u6790\u5668\u7684\u5de5\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4f1a\u89e6\u53d1\u4ee5\u4e0b\u4e8b\u4ef6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f00\u59cb\u6807\u7b7e\uff08Start Element\uff09<\/strong>\uff1a\u5f53\u89e3\u6790\u5668\u9047\u5230\u4e00\u4e2a\u5f00\u59cb\u6807\u7b7e\u65f6\uff0c\u4f1a\u89e6\u53d1\u4f60\u8bbe\u7f6e\u7684\u5f00\u59cb\u6807\u7b7e\u56de\u8c03\u51fd\u6570\uff08<code>startElement<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u675f\u6807\u7b7e\uff08End Element\uff09<\/strong>\uff1a\u5f53\u89e3\u6790\u5668\u9047\u5230\u4e00\u4e2a\u7ed3\u675f\u6807\u7b7e\u65f6\uff0c\u4f1a\u89e6\u53d1\u4f60\u8bbe\u7f6e\u7684\u7ed3\u675f\u6807\u7b7e\u56de\u8c03\u51fd\u6570\uff08<code>endElement<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5b57\u7b26\u6570\u636e\uff08Character Data\uff09<\/strong>\uff1a\u5f53\u89e3\u6790\u5668\u9047\u5230\u6807\u7b7e\u4e4b\u95f4\u7684\u5b57\u7b26\u6570\u636e\u65f6\uff0c\u4f1a\u89e6\u53d1\u4f60\u8bbe\u7f6e\u7684\u5b57\u7b26\u6570\u636e\u56de\u8c03\u51fd\u6570\uff08<code>characterData<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u58f0\u660e\uff08Declaring\uff09<\/strong>\uff1a\u5728\u5904\u7406 DOCTYPE \u6216\u5916\u90e8\u5b9e\u4f53\u65f6\uff0c\u89e3\u6790\u5668\u4f1a\u89e6\u53d1\u4e00\u4e9b\u58f0\u660e\u4e8b\u4ef6\uff0c\u901a\u5e38\u4e0d\u5e38\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>\u5e38\u7528\u51fd\u6570\u53ca\u8be6\u7ec6\u8bf4\u660e<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>xml_set_element_handler()<\/code><\/strong>\uff1a\u7528\u4e8e\u8bbe\u7f6e XML \u5143\u7d20\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u7b7e\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong><code>xml_set_character_data_handler()<\/code><\/strong>\uff1a\u8bbe\u7f6e\u5b57\u7b26\u6570\u636e\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong><code>xml_get_error_code()<\/code><\/strong>\uff1a\u83b7\u53d6\u5f53\u524d\u89e3\u6790\u5668\u7684\u9519\u8bef\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong><code>xml_parser_free()<\/code><\/strong>\uff1a\u91ca\u653e XML \u89e3\u6790\u5668\u7684\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u4f18\u52bf\u4e0e\u5c40\u9650\u6027<\/strong><\/h2>\n\n\n\n<p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9ad8\u6548<\/strong>\uff1aExpat \u662f\u57fa\u4e8e\u4e8b\u4ef6\u7684\u89e3\u6790\u5668\uff0c\u9002\u5408\u5904\u7406\u5927\u578b XML \u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u6d88\u8017\u4f4e<\/strong>\uff1a\u89e3\u6790\u65f6\u4e0d\u9700\u8981\u5c06\u6574\u4e2a XML \u6587\u6863\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u9002\u5408\u7528\u4e8e\u5904\u7406\u975e\u5e38\u5927\u7684 XML \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u4e0d\u540c\u7684 XML \u5143\u7d20\u548c\u5c5e\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5c40\u9650\u6027\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6ca1\u6709\u5185\u5b58\u6a21\u578b<\/strong>\uff1a\u4e0e DOM \u89e3\u6790\u5668\u4e0d\u540c\uff0cExpat \u89e3\u6790\u5668\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u5185\u5b58\u6811\u7ed3\u6784\uff0c\u56e0\u6b64\u4f60\u65e0\u6cd5\u50cf DOM \u89e3\u6790\u5668\u90a3\u6837\u968f\u673a\u8bbf\u95ee XML \u4e2d\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><strong>\u53ea\u80fd\u5904\u7406\u6d41\u5f0f\u6570\u636e<\/strong>\uff1aExpat \u89e3\u6790\u5668\u4e00\u6b21\u53ea\u80fd\u89e3\u6790\u4e00\u4e2a\u5757\u7684\u6570\u636e\uff0c\u56e0\u6b64\u9700\u8981\u5206\u6bb5\u5904\u7406\u5927\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5b98\u65b9\u6587\u6863\u94fe\u63a5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/book.xml.php\">PHP \u5b98\u65b9\u6587\u6863 &#8211; XML Expat<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/libexpat.github.io\/\">Expat \u89e3\u6790\u5668 &#8211; XML \u89e3\u6790\u5668\u4ecb\u7ecd<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u6587\u6863\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 Expat \u89e3\u6790\u5668\uff0c\u5e76\u9605\u8bfb\u66f4\u590d\u6742\u7684\u4f8b\u5b50\u548c\u4f18\u5316\u6280\u5de7\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u7684 XML Expat \u89e3\u6790\u5668\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684 XML \u89e3\u6790\u5668\u3002\u5b83\u7528\u4e8e\u89e3\u6790 XML \u6587\u6863\u5e76\u5c06\u89e3\u6790\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3125,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3124"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":3126,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3124\/revisions\/3126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3125"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}