{"id":236201,"date":"2022-02-27T09:36:36","date_gmt":"2022-02-27T01:36:36","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=236201"},"modified":"2022-02-21T15:37:29","modified_gmt":"2022-02-21T07:37:29","slug":"of-accessing-nodes","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/of-accessing-nodes.html","title":{"rendered":"XML DOM – \u8bbf\u95ee\u8282\u70b9\u6982\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u901a\u8fc7 DOM\uff0c\u80fd\u591f\u8bbf\u95ee XML \u6587\u6863\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u8bbf\u95ee\u8282\u70b9<\/strong><\/div>\n

\u60a8\u53ef\u4ee5\u901a\u8fc7\u4e09\u79cd\u65b9\u5f0f\u6765\u8bbf\u95ee\u8282\u70b9\uff1a<\/p>\n

    \n
  1. \u901a\u8fc7\u4f7f\u7528 getElementsByTagName() \u65b9\u6cd5\u3002<\/li>\n
  2. \u901a\u8fc7\u5faa\u73af\uff08\u904d\u5386\uff09\u8282\u70b9\u6811\u3002<\/li>\n
  3. \u901a\u8fc7\u5229\u7528\u8282\u70b9\u7684\u5173\u7cfb\u5728\u8282\u70b9\u6811\u4e2d\u5bfc\u822a\u3002<\/li>\n<\/ol>\n
    getElementsByTagName() \u65b9\u6cd5<\/strong><\/div>\n

    getElementsByTagName() \u8fd4\u56de\u62e5\u6709\u6307\u5b9a\u6807\u7b7e\u540d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n

    \u8bed\u6cd5<\/p>\n

    node.getElementsByTagName(\"tagname\");<\/pre>\n

    \u5b9e\u4f8b<\/p>\n

    \u4e0b\u9762\u7684\u5b9e\u4f8b\u8fd4\u56de x \u5143\u7d20\u4e0b\u7684\u6240\u6709 <title> \u5143\u7d20\uff1a<\/p>\n

    x.getElementsByTagName(\"title\");<\/pre>\n

    \u8bf7\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u5b9e\u4f8b\u4ec5\u8fd4\u56de x \u8282\u70b9\u4e0b\u7684 <title>\u5143\u7d20\u3002\u5982\u9700\u8fd4\u56de XML \u6587\u6863\u4e2d\u7684\u6240\u6709 <title> \u5143\u7d20\uff0c\u8bf7\u4f7f\u7528\uff1a<\/p>\n

    xmlDoc.getElementsByTagName(\"title\");<\/pre>\n

    \u5728\u8fd9\u91cc\uff0cxmlDoc \u5c31\u662f\u6587\u6863\u672c\u8eab\uff08\u6587\u6863\u8282\u70b9\uff09\u3002<\/p>\n

    DOM \u8282\u70b9\u5217\u8868\uff08Node List\uff09<\/strong><\/div>\n

    getElementsByTagName() \u65b9\u6cd5\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002\u8282\u70b9\u5217\u8868\u662f\u8282\u70b9\u7684\u6570\u7ec4\u3002<\/p>\n

    \u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d\uff0c\u7136\u540e\u5728\u53d8\u91cf x \u4e2d\u5b58\u50a8 <title>\u8282\u70b9\u7684\u4e00\u4e2a\u5217\u8868\uff1a<\/p>\n

    xmlDoc=loadXMLDoc(\"books.xml\");\r\n\r\nx=xmlDoc.getElementsByTagName(\"title\");<\/pre>\n

    \u53ef\u901a\u8fc7\u7d22\u5f15\u53f7\u8bbf\u95ee x \u4e2d\u7684 <title> \u5143\u7d20\u3002\u5982\u9700\u8bbf\u95ee\u7b2c\u4e09\u4e2a<title>\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\uff1a<\/p>\n

    y=x[2];<\/pre>\n

    \u6ce8\u610f\uff1a\u8be5\u7d22\u5f15\u4ece 0 \u5f00\u59cb\u3002<\/p>\n

    \u5728\u672c\u6559\u7a0b\u540e\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u66f4\u591a\u6709\u5173\u8282\u70b9\u5217\u8868\uff08Node List\uff09\u7684\u77e5\u8bc6\u3002<\/p>\n

    DOM \u8282\u70b9\u5217\u8868\u957f\u5ea6\uff08Node List Length\uff09<\/strong><\/div>\n

    length \u5c5e\u6027\u5b9a\u4e49\u8282\u70b9\u5217\u8868\u7684\u957f\u5ea6\uff08\u5373\u8282\u70b9\u7684\u6570\u91cf\uff09\u3002<\/p>\n

    \u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 length \u5c5e\u6027\u6765\u904d\u5386\u8282\u70b9\u5217\u8868\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    xmlDoc=<\/span>loadXMLDoc<\/span>(<\/span>\"<\/span>books.xml<\/span>\"<\/span>)<\/span>; \r\n<\/span>x<\/span>=<\/span>xmlDoc<\/span>.<\/span>getElementsByTagName<\/span>(<\/span>\"<\/span>title<\/span>\"<\/span>)<\/span>;\r\n<\/span>for<\/span> <\/span>(<\/span>i<\/span>=<\/span>0<\/span>;<\/span>i<\/span><<\/span>x<\/span>.<\/span>length<\/span>;<\/span>i<\/span>++<\/span>)<\/span>\r\n<\/span>{<\/span>\r\n  <\/span>document<\/span>.<\/span>write<\/span>(<\/span>x<\/span>[<\/span>i<\/span>]<\/span>.<\/span>childNodes<\/span>[<\/span>0<\/span>]<\/span>.<\/span>nodeValue<\/span>)<\/span>;\r\n  <\/span>document<\/span>.<\/span>write<\/span>(<\/span>\"<\/span>\"<\/span>)<\/span>;\r\n<\/span>}<\/span><\/pre>\n

    \u5b9e\u4f8b\u89e3\u91ca\uff1a<\/p>\n

      \n
    1. \u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n
    2. \u83b7\u53d6\u6240\u6709 \u5143\u7d20\u8282\u70b9<\/title><\/li>\n<li>\u8f93\u51fa\u6bcf\u4e2a <title> \u5143\u7d20\u7684\u6587\u672c\u8282\u70b9\u7684\u503c<\/title><\/li>\n<\/ol>\n<div id="j6sjwn9j3u" style=\"font-size: 14pt; color: white; background-color: black; border-left: red 10px solid; padding-left: 14px; margin-bottom: 20px; margin-top: 20px;\"><strong>\u8282\u70b9\u7c7b\u578b\uff08Node Types\uff09<\/strong><\/div>\n<p>XML \u6587\u6863\u7684 documentElement \u5c5e\u6027\u662f\u6839\u8282\u70b9\u3002<\/p>\n<p>\u8282\u70b9\u7684 nodeName \u5c5e\u6027\u662f\u8282\u70b9\u7684\u540d\u79f0\u3002<\/p>\n<p>\u8282\u70b9\u7684 nodeType \u5c5e\u6027\u662f\u8282\u70b9\u7684\u7c7b\u578b\u3002<\/p>\n<div id="j6sjwn9j3u" style=\"font-size: 14pt; color: white; background-color: black; border-left: red 10px solid; padding-left: 14px; margin-bottom: 20px; margin-top: 20px;\"><strong>\u904d\u5386\u8282\u70b9<\/strong><\/div>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u904d\u5386\u6839\u8282\u70b9\u7684\u5b50\u8282\u70b9\uff0c\u540c\u65f6\u4e5f\u662f\u5143\u7d20\u8282\u70b9\uff1a<\/p>\n<p>\u5b9e\u4f8b<\/p>\n<pre>xmlDoc<span class=\"hl-code\">=<\/span><span class=\"hl-identifier\">loadXMLDoc<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-string\">books.xml<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">; \r\n<\/span><span class=\"hl-identifier\">x<\/span><span class=\"hl-code\">=<\/span><span class=\"hl-identifier\">xmlDoc<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">documentElement<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">childNodes<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-reserved\">for<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\">=<\/span><span class=\"hl-number\">0<\/span><span class=\"hl-code\">;<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\"><<\/span><span class=\"hl-identifier\">x<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">length<\/span><span class=\"hl-code\">;<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\">++<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">\r\n<\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-reserved\">if<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">x<\/span><span class=\"hl-brackets\">[<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-brackets\">]<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">nodeType<\/span><span class=\"hl-code\">==<\/span><span class=\"hl-number\">1<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-comment\">\/\/<\/span><span class=\"hl-comment\"> \u6267\u884c\u4e00\u6b21<\/span><span class=\"hl-comment\"><\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-builtin\">document<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">write<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">x<\/span><span class=\"hl-brackets\">[<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-brackets\">]<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">nodeName<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n <\/span><span class=\"hl-builtin\">document<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">write<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n <\/span><span class=\"hl-brackets\">}<\/span><span class=\"hl-code\">\r\n<\/span><span class=\"hl-brackets\">}<\/span><\/pre>\n<p>\u5b9e\u4f8b\u89e3\u91ca\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n<li>\u83b7\u53d6\u6839\u5143\u7d20\u7684\u5b50\u8282\u70b9<\/li>\n<li>\u68c0\u67e5\u6bcf\u4e2a\u5b50\u8282\u70b9\u7684\u8282\u70b9\u7c7b\u578b\u3002\u5982\u679c\u8282\u70b9\u7c7b\u578b\u662f \"1\"\uff0c\u5219\u662f\u5143\u7d20\u8282\u70b9<\/li>\n<li>\u5982\u679c\u662f\u5143\u7d20\u8282\u70b9\uff0c\u5219\u8f93\u51fa\u8282\u70b9\u7684\u540d\u79f0<\/li>\n<\/ol>\n<div id="j6sjwn9j3u" style=\"font-size: 14pt; color: white; background-color: black; border-left: red 10px solid; padding-left: 14px; margin-bottom: 20px; margin-top: 20px;\"><strong>\u5bfc\u822a\u8282\u70b9\u7684\u5173\u7cfb<\/strong><\/div>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u8282\u70b9\u5173\u7cfb\u5bfc\u822a\u8282\u70b9\u6811\uff1a<\/p>\n<p>\u5b9e\u4f8b<\/p>\n<pre>xmlDoc<span class=\"hl-code\">=<\/span><span class=\"hl-identifier\">loadXMLDoc<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-string\">books.xml<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-identifier\">x<\/span><span class=\"hl-code\">=<\/span><span class=\"hl-identifier\">xmlDoc<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">getElementsByTagName<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-string\">book<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-brackets\">[<\/span><span class=\"hl-number\">0<\/span><span class=\"hl-brackets\">]<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">childNodes<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-identifier\">y<\/span><span class=\"hl-code\">=<\/span><span class=\"hl-identifier\">xmlDoc<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">getElementsByTagName<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-string\">book<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-brackets\">[<\/span><span class=\"hl-number\">0<\/span><span class=\"hl-brackets\">]<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">firstChild<\/span><span class=\"hl-code\">; \r\n<\/span><span class=\"hl-reserved\">for<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\">=<\/span><span class=\"hl-number\">0<\/span><span class=\"hl-code\">;<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\"><<\/span><span class=\"hl-identifier\">x<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">length<\/span><span class=\"hl-code\">;<\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\">++<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">\r\n<\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-reserved\">if<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">y<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">nodeType<\/span><span class=\"hl-code\">==<\/span><span class=\"hl-number\">1<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-comment\">\/\/<\/span><span class=\"hl-comment\"> \u8f93\u51fa\u8282\u70b9\u540d<\/span><span class=\"hl-comment\"><\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-builtin\">document<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">write<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">y<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">nodeName<\/span><span class=\"hl-code\"> + <\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-quotes\">\"<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n <\/span><span class=\"hl-brackets\">}<\/span><span class=\"hl-code\">\r\n <\/span><span class=\"hl-identifier\">y<\/span><span class=\"hl-code\">=<\/span><span class=\"hl-identifier\">y<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">nextSibling<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-brackets\">}<\/span><\/pre>\n<ol>\n<li>\u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n<li>\u83b7\u53d6\u7b2c\u4e00\u4e2a book \u5143\u7d20\u7684\u5b50\u8282\u70b9<\/li>\n<li>\u628a \"y\" \u53d8\u91cf\u8bbe\u7f6e\u4e3a\u7b2c\u4e00\u4e2a book \u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u5b50\u8282\u70b9\uff08\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9\u4ece \"y\" \u5f00\u59cb\uff09\uff0c\u68c0\u67e5\u8282\u70b9\u7c7b\u578b\uff0c\u5982\u679c\u8282\u70b9\u7c7b\u578b\u4e3a \"1\"\uff0c\u5219\u662f\u5143\u7d20\u8282\u70b9<\/li>\n<li>\u5982\u679c\u662f\u5143\u7d20\u8282\u70b9\uff0c\u5219\u8f93\u51fa\u8be5\u8282\u70b9\u7684\u540d\u79f0<\/li>\n<li>\u628a \"y\" \u53d8\u91cf\u8bbe\u7f6e\u4e3a\u4e0b\u4e00\u4e2a\u540c\u7ea7\u8282\u70b9\uff0c\u5e76\u518d\u6b21\u8fd0\u884c\u5faa\u73af<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u4e09\u79cd\u65b9\u5f0f\u6765\u8bbf\u95ee\u8282\u70b9\uff1a \u901a\u8fc7\u4f7f\u7528 getElementsByTagName() \u65b9\u6cd5\u3002 \u901a\u8fc7\u5faa\u73af\uff08\u904d\u5386 […]<\/p>\n","protected":false},"author":1481,"featured_media":236204,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-236201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=236201"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236201\/revisions"}],"predecessor-version":[{"id":236208,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236201\/revisions\/236208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/236204"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=236201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=236201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=236201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}