{"id":43160,"date":"2024-12-01T08:06:24","date_gmt":"2024-12-01T00:06:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43160\/"},"modified":"2024-12-01T08:06:24","modified_gmt":"2024-12-01T00:06:24","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-go-libxml2-%e6%9b%b4%e6%96%b0%e7%89%b9%e5%ae%9a-xml-%e5%85%83%e7%b4%a0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43160\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c<\/span><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 10:09:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u4f7f\u7528 go-libxml2 \u66f4\u65b0 xml \u5143\u7d20\u503c\u3002\u8be5\u5143\u7d20\u53ef\u80fd\u51fa\u73b0\u5728 xml \u6587\u6863\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684 xml \u4e2d\uff0c\u6211\u60f3\u66f4\u65b0\u4e00\u4e2a\u5143\u7d20<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n - &lt;note&gt;\n       &lt;to&gt;tove&lt;\/to&gt;\n       &lt;from&gt;jani&lt;\/ffrom&gt;\n       &lt;heading&gt;reminder&lt;\/heading&gt;\n       &lt;body&gt;don't forget me this weekend!&lt;\/body&gt;\n        &lt;url&gt;http:\/\/example.com&lt;\/url&gt;\n        &lt;links&gt;\n            &lt;url&gt;http:\/\/link1.com&lt;\/url&gt;\n            &lt;url&gt;http:\/\/link2.com&lt;\/url&gt;\n        &lt;\/links&gt;\n   &lt;\/note&gt;<\/pre>\n<p>\u6211\u60f3\u5728\u6240\u6709\u503c\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u67e5\u8be2\u53c2\u6570\u3002\u6240\u4ee5\u6211\u7684\u7ed3\u679c xml \u5c06\u5982\u4e0b\u6240\u793a<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n - &lt;note&gt;\n       &lt;to&gt;Tove&lt;\/to&gt;\n       &lt;from&gt;Jani&lt;\/Ffrom&gt;\n       &lt;heading&gt;Reminder&lt;\/heading&gt;\n       &lt;body&gt;Don't forget me this weekend!&lt;\/body&gt;\n        &lt;url&gt;http:\/\/example.com?param=value&lt;\/url&gt;\n        &lt;links&gt;\n            &lt;url&gt;http:\/\/link1.com?param=value&lt;\/url&gt;\n            &lt;url&gt;http:\/\/link2.com?param=value&lt;\/url&gt;\n        &lt;\/links&gt;\n   &lt;\/note&gt;<\/pre>\n<p>\u5982\u4f55\u4f7f\u7528go-libxml2\u4fee\u6539xml\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4f60\u4e0d\u80fd\u3002 go-libxml2 \u53ef\u4ee5\u52a0\u8f7d xml \u683c\u5f0f\u7684\u6587\u672c\u5e76\u4e0e\u8282\u70b9\u4e00\u8d77\u4f7f\u7528\uff0c\u4f46\u5b83\u6ca1\u6709\u5e8f\u5217\u5316\u4e3a xml \u683c\u5f0f\u7684\u6587\u672c\u7684\u529f\u80fd\u3002<\/p>\n<p>\u6216\u8005\u60a8\u9700\u8981\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5c06\u8282\u70b9\u6811\u8f6c\u6362\u4e3a xml \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8bdd\u867d\u5982\u6b64\uff0c\u60a8\u53ef\u4ee5\u6309\u5982\u4e0b\u65b9\u5f0f\u52a0\u8f7d\u548c\u4fee\u6539\u7f51\u5740\uff1a<\/p>\n<pre>doc, err := libxml2.ParseString(xmlstring)\n\nnodes := xpath.NodeList(doc.Find(`\/\/url`))\nfor i := 0; i &lt; len(nodes); i++ {\n    newvalue := nodes[i].NodeValue() + \"?param=value\"\n    nodes[i].SetNodeValue(newvalue)\n}\n\n\/\/ But here you cannot serialize back the 'doc' tree to XML with go-libxml2.\n\/\/ You need to code on your own.<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c \u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c \u6765\u6e90\uff1astackoverflow 2024-04-30 10:09:35 0\u6d4f\u89c8 \u6536\u85cf \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 go-libxml2 \u66f4\u65b0\u7279\u5b9a XML \u5143\u7d20\u503c\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u95ee\u9898\u5185\u5bb9 \u6211\u60f3\u4f7f\u7528 go-libxml2 \u66f4\u65b0 xml \u5143\u7d20\u503c\u3002\u8be5\u5143\u7d20\u53ef\u80fd\u51fa\u73b0\u5728 xml \u6587\u6863\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684 xml \u4e2d\uff0c\u6211\u60f3\u66f4\u65b0\u4e00\u4e2a\u5143\u7d20 &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt; &#8211; &lt;note&gt; &lt;to&gt;tove&lt;\/to&gt; &lt;from&gt;jani&lt;\/ffrom&gt; &lt;heading&gt;reminder&lt;\/heading&gt; &lt;body&gt;don&#8217;t forget me this weekend!&lt;\/body&gt; &lt;url&gt;http:\/\/example.com&lt;\/url&gt; &lt;links&gt; &lt;url&gt;http:\/\/link1.com&lt;\/url&gt; &lt;url&gt;http:\/\/link2.com&lt;\/url&gt; &lt;\/links&gt; &lt;\/note&gt; \u6211\u60f3\u5728\u6240\u6709\u503c\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u67e5\u8be2\u53c2\u6570\u3002\u6240\u4ee5\u6211\u7684\u7ed3\u679c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43160","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43160","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=43160"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43160\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}