{"id":3800,"date":"2024-11-10T15:27:01","date_gmt":"2024-11-10T07:27:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3800\/"},"modified":"2024-11-10T15:27:01","modified_gmt":"2024-11-10T07:27:01","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-ajax-%e8%af%bb%e5%8f%96-xml-%e5%b9%b6%e6%98%be%e7%a4%ba%e5%ad%90%e8%8a%82%e7%82%b9%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3800\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Ajax \u8bfb\u53d6 XML \u5e76\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173114321618891.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Ajax \u8bfb\u53d6 XML \u5e76\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Ajax \u8bfb\u53d6 XML \u5e76\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 ajax \u8bfb\u53d6 xml \u5e76\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e<\/strong><\/p>\n<p>\u901a\u8fc7 ajax \u8bfb\u53d6 xml \u662f\u4e00\u79cd\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u5728\u8bfb\u53d6 xml \u540e\uff0c\u901a\u5e38\u9700\u8981\u663e\u793a\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\u3002\u5728\u672c\u95ee\u7b54\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528 javascript \u8bfb\u53d6 xml \u5e76\u663e\u793a\u5176\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u8bfb\u53d6 xml<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u4f7f\u7528 xmlhttprequest \u5bf9\u8c61\u8bfb\u53d6 xml \u6587\u4ef6\u3002\u53ef\u4ee5\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const xhr = new xmlhttprequest();\nxhr.open(\"get\", \"path\/to\/xml_file.xml\", true);\nxhr.onreadystatechange = function() {\n  if (this.readystate === 4 &amp;&amp; this.status === 200) {\n    \/\/ xml \u5df2\u52a0\u8f7d\u6210\u529f\n    const xmldoc = this.responsexml;\n    \/\/ ...\n  }\n};\nxhr.send();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u5b50\u8282\u70b9\u6570\u636e<\/strong><\/p>\n<p>\u8bfb\u53d6 xml \u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 getelementsbytagname() \u65b9\u6cd5\u83b7\u53d6\u7279\u5b9a\u6807\u7b7e\u7684\u5b50\u8282\u70b9\u3002\u4f8b\u5982\uff0c\u8981\u83b7\u53d6\u540d\u4e3a &#8220;item&#8221; \u7684\u6807\u7b7e\u7684\u6240\u6709\u5b50\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const items = xmldoc.getelementsbytagname(\"item\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e<\/strong><\/p>\n<p>\u83b7\u53d6\u5b50\u8282\u70b9\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 textcontent \u5c5e\u6027\u6765\u83b7\u53d6\u5176\u6587\u672c\u5185\u5bb9\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 innerhtml \u5c5e\u6027\u5c06\u6570\u636e\u663e\u793a\u5728 html \u5143\u7d20\u4e2d\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u524d\u4e24\u6761\u8bb0\u5f55\u7684\u6587\u672c\u5185\u5bb9\u663e\u793a\u5728 html \u5143\u7d20\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const firstItemText = items[0].textContent;\nconst secondItemText = items[1].textContent;\n\ndocument.getElementById(\"first-item\").innerHTML = firstItemText;\ndocument.getElementById(\"second-item\").innerHTML = secondItemText;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528 ajax \u8bfb\u53d6 xml \u5e76\u663e\u793a\u5176\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\uff0c\u4ece\u800c\u4e3a web \u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u52a8\u6001\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Ajax \u8bfb\u53d6 XML \u5e76\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 ajax \u8bfb\u53d6 xml \u5e76\u663e\u793a\u5b50\u8282\u70b9\u6570\u636e \u901a\u8fc7 ajax \u8bfb\u53d6 xml \u662f\u4e00\u79cd\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u5728\u8bfb\u53d6 xml \u540e\uff0c\u901a\u5e38\u9700\u8981\u663e\u793a\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\u3002\u5728\u672c\u95ee\u7b54\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528 javascript \u8bfb\u53d6 xml \u5e76\u663e\u793a\u5176\u5b50\u8282\u70b9\u4e2d\u7684\u6570\u636e\u3002 \u8bfb\u53d6 xml \u9996\u5148\uff0c\u9700\u8981\u4f7f\u7528 xmlhttprequest \u5bf9\u8c61\u8bfb\u53d6 xml \u6587\u4ef6\u3002\u53ef\u4ee5\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a const xhr = new xmlhttprequest(); xhr.open(&#8220;get&#8221;, &#8220;path\/to\/xml_file.xml&#8221;, true); xhr.onreadystatechange = function() { if (this.readystate === 4 &amp;&amp; this.status === 200) { \/\/ xml \u5df2\u52a0\u8f7d\u6210\u529f const xmldoc = this.responsexml; \/\/ &#8230; } }; [&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-3800","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3800","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=3800"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3800\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}