{"id":548,"date":"2009-07-28T18:01:00","date_gmt":"2009-07-28T10:01:00","guid":{"rendered":""},"modified":"2013-11-27T20:34:58","modified_gmt":"2013-11-27T12:34:58","slug":"javasript%e8%af%bb%e5%86%99xml","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/548.html","title":{"rendered":"JavaSript\u8bfb\u5199XML"},"content":{"rendered":"<p>load \u65b9\u6cd5<\/p>\n<div>\n<p>\u4f5c \u7528<br \/>\n\u8868\u793a\u4ece\u6307\u5b9a\u4f4d\u7f6e\u52a0\u8f7d\u7684\u6587\u4ef6\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nboolValue \uff1d xmlDocument.load(url);<\/p>\n<p>\u8bf4 \u660e<br \/>\nurl \u5305\u542b\u8981\u88ab\u52a0\u8f7d\u6863\u6848\u7684URL \u7684\u5b57\u7b26\u4e32\u3002\u5047\u5982\u6587\u4ef6\u52a0\u8f7d\u6210\u529f\uff0c\u4f20\u56de\u503c\u5373\u4e3atrue\u3002\u82e5\u52a0\u8f7d\u5931\u8d25\uff0c\u4f20\u56de\u503c\u4e3afalse\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nboolValue \uff1d xmlDoc.load(&#8220;LstA_1.xml&#8221;);<br \/>\nalert(boolValue);<\/p>\n<p>loadXML \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u52a0\u8f7d\u4e00\u4e2aXML \u6587\u4ef6\u6216\u5b57\u7b26\u4e32\u7684\u7247\u65ad\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nboolValue \uff1d xmlDocument.loadXML(xmlString);<\/p>\n<p>\u8bf4 \u660e<br \/>\nxmlString \u662f\u5305\u542bXML \u6587\u5b57\u7801\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nxmlString \uff1d &#8220;&lt;GREETING&gt;&lt;MESSAGE&gt;Hello!&lt;\/MESSAGE&gt;&lt;\/GREETING&gt;&#8221;;<br \/>\nboolValue \uff1d xmlDoc.loadXML(xmlString);<br \/>\nalert(boolValue);<\/p>\n<p>documentElement \u5c5e\u6027<\/p>\n<p>\u4f5c \u7528<br \/>\n\u786e\u8ba4XML \u6587\u4ef6\u7684\u6839\uff08Root\uff09\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjDoc=xmlDocument.documentElement;<\/p>\n<p>\u8bf4 \u660e<br \/>\n\u56de\u4e00\u4e2a\u5728\u5355\u4e00\u6839\u6587\u4ef6\u5143\u7d20\u4e2d\u5305\u542b\u6570\u636e\u7684\u5bf9\u8c61\u3002\u6b64\u5c5e\u6027\u53ef\u8bfb\/\u5199\uff0c\u5982\u679c\u6587\u4ef6\u4e2d\u4e0d\u5305\u542b\u6839\u8282\u70b9\uff0c\u5c06\u4f20\u56denull\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjDocRoot \uff1d xmlDoc.documentElement;<br \/>\nalert(objDocRoot);<\/p>\n<p>childNodes \u5c5e\u6027<\/p>\n<p>\u4f5c \u7528<br \/>\n\u4f20\u56de\u4e00\u4e2a\u8282\u70b9\u6e05\u5355\uff0c\u5305\u542b\u8be5\u8282\u70b9\u6240\u6709\u53ef\u7528\u7684\u5b50\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjNodeList=node.childNodes;<\/p>\n<p>\u8bf4 \u660e<br \/>\n\u4f20\u56de\u4e00\u4e2a\u7269\u4ef6\u3002\u5047\u5982\u8fd9\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u4f20\u56denull\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjNodeList \uff1d xmlDoc.childNodes;<br \/>\nalert(objNodeList);<\/p>\n<p>attribute \u5c5e\u6027<\/p>\n<p>\u4f5c \u7528<br \/>\n\u4f20\u56de\u76ee\u524d\u8282\u70b9\u7684\u5c5e\u6027\u5217\u8868\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjAttributeList = xmlNode.attributes;<\/p>\n<p>\u8bf4 \u660e<br \/>\n\u4f20\u56de\u4e00\u4e2a\u7269\u4ef6\u3002\u5982\u679c\u6b64\u8282\u70b9\u4e0d\u80fd\u5305\u542b\u5c5e\u6027\uff0c\u5219\u4f20\u56de\u7a7a\u503c\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjAttList \uff1d xmlDoc.documentElement.attributes;<br \/>\nalert(objAttList);<\/p>\n<p>createNode \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u5efa\u7acb\u4e00\u4e2a\u6307\u5b9a\u578b\u6001\u3001\u540d\u79f0\uff0c\u53ca\u547d\u540d\u7a7a\u95f4\u7684\u65b0\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nxmlDocument.createNode(type, name, nameSpaceURI);<\/p>\n<p>\u8bf4 \u660e<br \/>\ntype \u7528\u6765\u786e\u8ba4\u8981\u88ab\u5efa\u7acb\u7684\u8282\u70b9\u578b\u6001\uff0cname \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u786e\u8ba4\u65b0\u8282\u70b9\u7684\u540d\u79f0\uff0c\u547d\u540d\u7a7a\u95f4\u7684\u524d\u7f00\u5219\u662f\u9009\u62e9\u6027\u7684\u3002nameSpaceURI \u662f\u4e00\u4e2a\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4URI \u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u524d\u7f00\u88ab\u5305\u542b\u5728\u540d\u79f0\u53c2\u6570\u4e2d\uff0c\u6b64\u8282\u70b9\u4f1a\u5728nameSpaceURI \u7684\u5185\u6587\u4e2d\u4ee5\u6307\u5b9a\u7684\u524d\u7f00\u5efa\u7acb\u3002\u5982\u679c\u4e0d\u5305\u542b\u524d\u7f00\uff0c\u6307\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4f1a\u88ab\u89c6\u4e3a\u9884\u8bbe\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjNewNode \uff1d xmlDoc.createNode(1, &#8220;TO&#8221;, &#8220;&#8221;);<br \/>\nalert(objNewNode.xml);<\/p>\n<p>createTextNode \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684text \u8282\u70b9\uff0c\u5e76\u5305\u542b\u6307\u5b9a\u7684\u6570\u636e\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nxmlDocument.createTextNode(data);<\/p>\n<p>\u8bf4 \u660e<br \/>\ndata \u662f\u4e00\u4e2a\u4ee3\u8868\u65b0text \u8282\u70b9\u7684\u5b57\u7b26\u4e32\u3002\u4e00\u4e2a\u65b0\u7684text \u8282\u70b9\u88ab\u5efa\u7acb\uff0c\u4f46\u662f\u6ca1\u6709\u52a0\u5230\u6587\u4ef6\u6811\u4e2d\u3002\u82e5\u8981\u5c06\u8282\u70b9\u52a0\u5230\u6587\u4ef6\u6811\u4e2d\uff0c\u5fc5\u987b\u4f7f\u7528\u63d2\u5165\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1ainsertBefore\uff0creplaceChild\u6216appendChild\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjNewTextNode \uff1d xmlDoc.createTextNode(&#8220;This is a text node.&#8221;);<br \/>\nalert(objNewTextNode.xml);<\/p>\n<p>createElement \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u5efa\u7acb\u4e00\u4e2a\u6307\u5b9a\u540d\u79f0\u7684\u5143\u7d20\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nxmlDocument.createElement(tagName);<\/p>\n<p>\u8bf4 \u660e<br \/>\ntagName \u662f\u4e00\u4e2a\u533a\u5206\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6765\u6307\u5b9a\u65b0\u5143\u7d20\u540d\u79f0\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjNewElement \uff1d xmlDoc.createElement(&#8220;TO&#8221;);<br \/>\nalert(objNewElement.xml);<\/p>\n<p>AppendChild \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u52a0\u4e0a\u4e00\u4e2a\u8282\u70b9\u5f53\u4f5c\u6307\u5b9a\u8282\u70b9\u6700\u540e\u7684\u5b50\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nxmlDocumentNode.appendChild(newChild);<\/p>\n<p>\u8bf4 \u660e<br \/>\nnewChild \u662f\u9644\u52a0\u5b50\u8282\u70b9\u7684\u5730\u5740\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\ndocObj \uff1d xmlDoc.documentElement;<br \/>\nalert(docObj.xml);<br \/>\nobjNewNode \uff1d docObj.appendChild(xmlDoc.documentElement. firstChild);<br \/>\nalert(docObj.xml);<\/p>\n<p>removeChild \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u4f1a\u5c06\u6307\u5b9a\u7684\u8282\u70b9\u4ece\u8282\u70b9\u6e05\u5355\u4e2d\u79fb\u9664\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjDocumentNode \uff1d xmlDocumentNode.removeChild(oldChild);<\/p>\n<p>\u8bf4 \u660e<br \/>\noldChild \u4e3a\u4e00\u4e2a\u5305\u542b\u8981\u88ab\u79fb\u9664\u7684\u8282\u70b9\u5bf9\u8c61\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjRemoveNode \uff1d xmlDoc.documentElement.childNodes.item(3)\uff1b<br \/>\nalert(xmlDoc.xml);<br \/>\nxmlDoc.documentElement.removeChild(objRemoveNode)\uff1b<br \/>\nalert(xmlDoc.xml)\uff1b<\/p>\n<p>replaceChild \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u7f6e\u6362\u6307\u5b9a\u7684\u65e7\u5b50\u8282\u70b9\u4e3a\u63d0\u4f9b\u7684\u65b0\u5b50\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjDocumentNode \uff1d xmlDocumentNode.replaceChild(newChild,oldChild)\uff1b<\/p>\n<p>\u8bf4 \u660e<br \/>\nnewChild \u4e3a\u5305\u542b\u65b0\u5b50\u8282\u70b9\u7684\u5bf9\u8c61\u3002\u5982\u679c\u6b64\u53c2\u6570\u4e3anull\uff0c\u5219\u6b64\u65e7\u5b50\u8282\u70b9\u4f1a\u88ab\u79fb\u9664\u800c\u4e0d\u4f1a\u88ab\u53d6\u4ee3\u3002oldChild \u4e3a\u5305\u542b\u65e7\u5b50\u8282\u70b9\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjOldNode \uff1d xmlDoc.documentElement.childNodes.item(3)\uff1b<br \/>\nobjNewNode \uff1d xmlDoc.createComment(&#8220;I&#8217;ve replaced the BCC element.&#8221;)\uff1b<br \/>\nalert(xmlDoc.xml)\uff1b<br \/>\nxmlDoc.documentElement.replaceChild(objNewNode,objOldNode)\uff1b<br \/>\nalert(xmlDoc.xml)\uff1b<\/p>\n<p>selectNodes \u65b9\u6cd5<\/p>\n<p>\u4f5c \u7528<br \/>\n\u4f20\u56de\u6240\u6709\u7b26\u5408\u63d0\u4f9b\u6837\u5f0f(pattern)\u7684\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjDocumentNodeList \uff1d xmlDocumentNode.selectNodes(patternString)\uff1b<\/p>\n<p>\u8bf4 \u660e<br \/>\npatternString \u4e3a\u4e00\u5305\u542bXSL \u6837\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u6b64\u65b9\u6cd5\u4f1a\u4f20\u56de\u8282\u70b9\u6e05\u5355\u5bf9\u8c61\uff0c\u5305\u542b\u7b26\u5408\u6837\u5f0f\u7684\u8282\u70b9\u3002\u5982\u679c\u6ca1\u6709\u7b26\u5408\u7684\u8282\u70b9\uff0c\u5219\u4f20\u56de\u7a7a\u7684\u6e05\u5355\u5217\u8868\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjNodeList\uff1dxmlDoc.selectNodes\uff08&#8221;\/&#8221;\uff09\uff1b<br \/>\nalert\uff08objNodeList.item(0).xml\uff09\uff1b<\/p>\n<p>nodeValue \u5c5e\u6027<\/p>\n<p>\u4f5c \u7528<br \/>\n\u4f20\u56de\u6307\u5b9a\u8282\u70b9\u76f8\u5173\u7684\u6587\u5b57\u3002\u8fd9\u5e76\u975e\u4e00\u4e2a\u5143\u7d20\u4e2d\u6570\u636e\u7684\u503c\uff0c\u800c\u662f\u4e0e\u4e00\u4e2a\u8282\u70b9\u76f8\u5173\u4e14\u672a\u89e3\u6790\u7684\u6587\u5b57\uff0c\u5c31\u50cf\u4e00\u4e2a\u5c5e\u6027\u6216\u8005\u4e00\u4e2a\u5904\u7406\u6307\u4ee4\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nvarNodeValue \uff1d xmlDocNode.nodeValue;<\/p>\n<p>\u8bf4 \u660e<br \/>\n\u4f20\u56de\u7684\u6587\u5b57\u4ee3\u8868\u4ee5\u8282\u70b9\u7684nodeType \u5c5e\u6027\u4e3a\u4e3b\u7684\u578b\u6001\u503c\u3002\uff08\u8bf7\u53c2\u8003\u9644\u5f55\u4e2d\u7684nodeType \u5c5e\u6027\u3002\uff09\u56e0\u4e3a\u8282\u70b9\u578b\u6001\u53ef\u80fd\u662f\u51e0\u79cd\u6570\u636e\u578b\u6001\u4e2d\u7684\u4e00\u79cd\uff0c\u4f20\u56de\u503c\u4e5f\u56e0\u6b64\u6709\u5dee\u5f02\u3002\u4f20\u56denull \u7684\u8282\u70b9\u578b\u6001\u6709\uff1aDOCUMENT\u3001ELEMENT\u3001DOCUMENT TYPE\u3001DOCUMENT FRAGMENT\u3001ENTITY\u3001ENTITY REFERENCE\uff0c\u548cNOTATION\u3002\u6b64\u5c5e\u6027\u53ef\u64e6\u5199\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nvarNodeValue \uff1d xmlDoc.documentElement.nodeValue;<br \/>\nalert(varNodeValue);<\/p>\n<p>nextSibling \u5c5e\u6027<\/p>\n<p>\u4f5c \u7528<br \/>\n\u5728\u76ee\u524d\u6587\u4ef6\u8282\u70b9\u7684\u5b50\u8282\u70b9\u5217\u8868\u4e2d\u4f20\u56de\u4e0b\u4e00\u4e2a\u5144\u5f1f\u8282\u70b9\u3002<br \/>\n\u57fa\u672c\u8bed\u6cd5<br \/>\nobjNextSibling \uff1d xmlDocNode.nextSibling;<\/p>\n<p>\u8bf4 \u660e<br \/>\n\u6b64\u5c5e\u6027\u662f\u53ea\u8bfb\u4e14\u4f20\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002\u5982\u679c\u8282\u70b9\u4e2d\u6ca1\u6709\u5305\u542b\u5176\u5b83\u7684\u76f8\u5173\u8282\u70b9\uff0c\u4f1a\u4f20\u56denull\u3002<\/p>\n<p>\u8303 \u4f8b<br \/>\nobjSibling \uff1d xmlDoc.documentElement.childNodes.item(1) .nextSibling;<br \/>\nalert(objSibling);<\/p>\n<p>\u8fd9\u4e9b\u591f\u4f60\u7528\u4e00\u9635\u5b50\u7684\u4e86\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>load \u65b9\u6cd5 \u4f5c \u7528 \u8868\u793a\u4ece\u6307\u5b9a\u4f4d\u7f6e\u52a0\u8f7d\u7684\u6587\u4ef6\u3002 \u57fa\u672c\u8bed\u6cd5 boolValue \uff1d xmlDocument. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=548"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":5276,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/548\/revisions\/5276"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}