{"id":47954,"date":"2024-12-02T12:08:20","date_gmt":"2024-12-02T04:08:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47954\/"},"modified":"2024-12-02T12:08:20","modified_gmt":"2024-12-02T04:08:20","slug":"php-%e9%80%92%e5%bd%92%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90-xml-%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47954\/","title":{"rendered":"PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863\uff1a\u521b\u5efa\u4e00\u4e2a XML \u89e3\u6790\u5668\u3002\u8bbe\u7f6e\u5143\u7d20\u5904\u7406\u5668\u6765\u5904\u7406\u5f00\u59cb\u3001\u7ed3\u675f\u5143\u7d20\u548c\u5b57\u7b26\u6570\u636e\u3002\u89e3\u6790 XML \u6587\u6863\u3002\u91ca\u653e\u89e3\u6790\u5668\u3002\u5b9e\u6218\u793a\u4f8b\uff1a\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863\u53ef\u8f7b\u677e\u904d\u5386\u5d4c\u5957\u7ed3\u6784\uff0c\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729945650671ce032e0552.jpg\" class=\"aligncenter\" title=\"PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863\u63d2\u56fe\" alt=\"PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u89e3\u6790 XML \u6587\u6863\u3002\u8fd9\u662f\u4e00\u79cd\u6709\u6548\u7684\u89e3\u6790\u5927\u578b\u548c\u590d\u6742 XML \u6587\u6863\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u5d4c\u5957\u7ed3\u6784\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>function parseXML($xml) {\n  $parser = xml_parser_create();\n  xml_set_element_handler($parser, \"startElement\", \"endElement\");\n  xml_set_character_data_handler($parser, \"characterData\");\n  xml_parse($parser, $xml);\n  xml_parser_free($parser);\n}\n\nfunction startElement($parser, $name, $attributes) {\n  echo \"Start element: $name\\n\";\n  foreach ($attributes as $key =&gt; $value) {\n    echo \"\\tAttribute: $key='$value'\\n\";\n  }\n}\n\nfunction endElement($parser, $name) {\n  echo \"End element: $name\\n\";\n}\n\nfunction characterData($parser, $data) {\n  echo \"Character data: $data\\n\";\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863\u7684\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>$xml = \"&lt;root&gt;\n  &lt;child1&gt;This is child 1&lt;\/child1&gt;\n  &lt;child2&gt;This is child 2\n    &lt;grandchild&gt;This is a grandchild&lt;\/grandchild&gt;\n  &lt;\/child2&gt;\n  &lt;child3&gt;This is child 3&lt;\/child3&gt;\n&lt;\/root&gt;\";\n\nparseXML($xml);<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre>Start element: root\n  Attribute: id='123'\nStart element: child1\nCharacter data: This is child 1\nEnd element: child1\nStart element: child2\nCharacter data: This is child 2\nStart element: grandchild\nCharacter data: This is a grandchild\nEnd element: grandchild\nEnd element: child2\nStart element: child3\nCharacter data: This is child 3\nEnd element: child3\nEnd element: root<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u5d4c\u5957\u7ed3\u6784\u5e76\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863 \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u89e3\u6790 XML \u6587\u6863\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863\uff1a\u521b\u5efa\u4e00\u4e2a XML \u89e3\u6790\u5668\u3002\u8bbe\u7f6e\u5143\u7d20\u5904\u7406\u5668\u6765\u5904\u7406\u5f00\u59cb\u3001\u7ed3\u675f\u5143\u7d20\u548c\u5b57\u7b26\u6570\u636e\u3002\u89e3\u6790 XML \u6587\u6863\u3002\u91ca\u653e\u89e3\u6790\u5668\u3002\u5b9e\u6218\u793a\u4f8b\uff1a\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863\u53ef\u8f7b\u677e\u904d\u5386\u5d4c\u5957\u7ed3\u6784\uff0c\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3002 PHP \u9012\u5f52\u51fd\u6570\u89e3\u6790 XML \u6587\u6863 \u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u89e3\u6790 XML \u6587\u6863\u3002\u8fd9\u662f\u4e00\u79cd\u6709\u6548\u7684\u89e3\u6790\u5927\u578b\u548c\u590d\u6742 XML \u6587\u6863\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u5d4c\u5957\u7ed3\u6784\u3002 \u4ee3\u7801\u793a\u4f8b function parseXML($xml) { $parser = xml_parser_create(); xml_set_element_handler($parser, &#8220;startElement&#8221;, &#8220;endElement&#8221;); xml_set_character_data_handler($parser, &#8220;characterData&#8221;); xml_parse($parser, $xml); xml_parser_free($parser); } function startElement($parser, $name, $attributes) { echo &#8220;Start element: $name\\n&#8221;; foreach ($attributes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47954","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47954"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47954\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}