{"id":240146,"date":"2022-04-22T09:57:39","date_gmt":"2022-04-22T01:57:39","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=240146"},"modified":"2022-04-11T10:59:18","modified_gmt":"2022-04-11T02:59:18","slug":"add-node-overview","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/add-node-overview.html","title":{"rendered":"XML DOM \u6dfb\u52a0\u8282\u70b9\u6982\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nappendChild() \u65b9\u6cd5\u5411\u4e00\u4e2a\u5df2\u6709\u7684\u8282\u70b9\u6dfb\u52a0\u4e00\u4e2a\u5b50\u8282\u70b9\u3002\u65b0\u8282\u70b9\u4f1a\u6dfb\u52a0\uff08\u8ffd\u52a0\uff09\u5230\u4efb\u4f55\u5df2\u6709\u7684\u5b50\u8282\u70b9\u4e4b\u540e\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6dfb\u52a0\u8282\u70b9 - appendChild()<\/strong><\/div>\n

appendChild() \u65b9\u6cd5\u5411\u4e00\u4e2a\u5df2\u6709\u7684\u8282\u70b9\u6dfb\u52a0\u4e00\u4e2a\u5b50\u8282\u70b9\u3002<\/p>\n

\u65b0\u8282\u70b9\u4f1a\u6dfb\u52a0\uff08\u8ffd\u52a0\uff09\u5230\u4efb\u4f55\u5df2\u6709\u7684\u5b50\u8282\u70b9\u4e4b\u540e\u3002<\/p>\n

\u6ce8\u610f\uff1a\u5982\u679c\u8282\u70b9\u7684\u4f4d\u7f6e\u5f88\u91cd\u8981\uff0c\u8bf7\u4f7f\u7528 insertBefore() \u65b9\u6cd5\u3002<\/p>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u521b\u5efa\u4e00\u4e2a\u5143\u7d20\uff08<edition>\uff09\uff0c\u5e76\u628a\u5b83\u6dfb\u52a0\u5230\u7b2c\u4e00\u4e2a <book> \u5143\u7d20\u7684\u6700\u540e\u4e00\u4e2a\u5b50\u8282\u70b9\u540e\u9762\uff1a<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

xmlDoc=loadXMLDoc(\"books.xml\");\r\nnewel=xmlDoc.createElement(\"edition\");\r\nx=xmlDoc.getElementsByTagName(\"book\")[0];\r\nx.appendChild(newel);<\/pre>\n

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

    \n
  1. \u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n
  2. \u521b\u5efa\u4e00\u4e2a\u65b0\u8282\u70b9 <edition><\/li>\n
  3. \u628a\u8fd9\u4e2a\u8282\u70b9\u8ffd\u52a0\u5230\u7b2c\u4e00\u4e2a <book> \u5143\u7d20<\/li>\n<\/ol>\n
    \u63d2\u5165\u8282\u70b9 - insertBefore()<\/strong><\/div>\n

    insertBefore()\u65b9\u6cd5\u7528\u4e8e\u5728\u6307\u5b9a\u7684\u5b50\u8282\u70b9\u4e4b\u524d\u63d2\u5165\u8282\u70b9\u3002<\/p>\n

    \u5728\u88ab\u6dfb\u52a0\u7684\u8282\u70b9\u7684\u4f4d\u7f6e\u5f88\u91cd\u8981\u65f6\uff0c\u6b64\u65b9\u6cd5\u5f88\u6709\u7528\uff1a<\/p>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    xmlDoc=loadXMLDoc(\"books.xml\");\r\nnewNode=xmlDoc.createElement(\"book\");\r\nx=xmlDoc.documentElement;\r\ny=xmlDoc.getElementsByTagName(\"book\")[3];\r\nx.insertBefore(newNode,y);<\/pre>\n

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

      \n
    1. \u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n
    2. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u8282\u70b9 <book><\/li>\n
    3. \u628a\u8fd9\u4e2a\u65b0\u8282\u70b9\u63d2\u5230\u6700\u540e\u4e00\u4e2a <book> \u5143\u7d20\u8282\u70b9\u4e4b\u524d<\/li>\n<\/ol>\n

      \u5982\u679c insertBefore() \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f null\uff0c\u65b0\u8282\u70b9\u5c06\u88ab\u6dfb\u52a0\u5230\u6700\u540e\u4e00\u4e2a\u5df2\u6709\u7684\u5b50\u8282\u70b9\u4e4b\u540e\u3002<\/p>\n

      x.insertBefore(newNode,null) \u548c x.appendChild(newNode) \u90fd\u53ef\u4ee5\u5411 x \u8ffd\u52a0\u4e00\u4e2a\u65b0\u7684\u5b50\u8282\u70b9\u3002<\/p>\n

      \u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong><\/div>\n

      addAtribute() \u8fd9\u4e2a\u65b9\u6cd5\u662f\u4e0d\u5b58\u5728\u7684\u3002<\/p>\n

      \u5982\u679c\u5c5e\u6027\u4e0d\u5b58\u5728\uff0c\u5219 setAttribute() \u53ef\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027\uff1a<\/p>\n

      \u5b9e\u4f8b<\/strong><\/p>\n

      xmlDoc=loadXMLDoc(\"books.xml\");\r\nx=xmlDoc.getElementsByTagName('book');\r\nx[0].setAttribute(\"edition\",\"first\");<\/pre>\n

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

        \n
      1. \u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n
      2. \u628a\u7b2c\u4e00\u4e2a <book> \u5143\u7d20\u7684 \"edition\" \u5c5e\u6027\u7684\u503c\u8bbe\u7f6e\uff08\u521b\u5efa\uff09\u4e3a \"first\"<\/li>\n<\/ol>\n

        \u6ce8\u610f\uff1a\u5982\u679c\u5c5e\u6027\u5df2\u5b58\u5728\uff0csetAttribute() \u65b9\u6cd5\u5c06\u8986\u76d6\u5df2\u6709\u7684\u503c\u3002<\/p>\n

        \u5411\u6587\u672c\u8282\u70b9\u6dfb\u52a0\u6587\u672c - insertData()<\/strong><\/div>\n

        insertData() \u65b9\u6cd5\u5c06\u6570\u636e\u63d2\u5165\u5df2\u6709\u7684\u6587\u672c\u8282\u70b9\u4e2d\u3002<\/p>\n

        insertData() \u65b9\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n

          \n
        1. offset - \u5728\u4f55\u5904\u5f00\u59cb\u63d2\u5165\u5b57\u7b26\uff08\u4ee5 0 \u5f00\u59cb\uff09<\/li>\n
        2. string - \u8981\u63d2\u5165\u7684\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n

          \u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u5c06\u628a \"Easy\" \u6dfb\u52a0\u5230\u5df2\u52a0\u8f7d\u7684 XML \u7684\u7b2c\u4e00\u4e2a <title> \u5143\u7d20\u7684\u6587\u672c\u8282\u70b9\uff1a<\/p>\n

          \u5b9e\u4f8b<\/strong><\/p>\n

          xmlDoc=loadXMLDoc(\"books.xml\");\r\nx=xmlDoc.getElementsByTagName(\"title\")[0].childNodes[0];\r\nx.insertData(0,\"Easy \");<\/pre>\n","protected":false},"excerpt":{"rendered":"

          appendChild() \u65b9\u6cd5\u5411\u4e00\u4e2a\u5df2\u6709\u7684\u8282\u70b9\u6dfb\u52a0\u4e00\u4e2a\u5b50\u8282\u70b9\u3002 \u65b0\u8282\u70b9\u4f1a\u6dfb\u52a0\uff08\u8ffd\u52a0\uff09\u5230\u4efb\u4f55\u5df2\u6709\u7684\u5b50\u8282\u70b9\u4e4b\u540e\u3002 […]<\/p>\n","protected":false},"author":1481,"featured_media":240135,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-240146","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\/240146","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=240146"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/240146\/revisions"}],"predecessor-version":[{"id":240148,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/240146\/revisions\/240148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/240135"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=240146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=240146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=240146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}