{"id":11324,"date":"2023-02-06T17:18:10","date_gmt":"2023-02-06T09:18:10","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=11324"},"modified":"2023-02-08T11:20:43","modified_gmt":"2023-02-08T03:20:43","slug":"xml","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2023\/02\/06\/xml\/","title":{"rendered":"go\u8bed\u8a00\u89e3\u6790xml\u6587\u4ef6"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u5c06xml\u6587\u4ef6\u53cd\u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4f53\u4e2d\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n        \"encoding\/xml\"\r\n        \"fmt\"\r\n        \"log\"\r\n)\r\n\r\/\/\u5b9a\u4e49\u5e38\u91cfs\uff0c\u503c\u4e3axml\u683c\u5f0f\u7684\u5185\u5bb9,\u7528\u53cd\u5f15\u53f7\u540e\u53ef\u4ee5\u5b9e\u73b0\u6362\u884c\nconst s = `&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>&lt;html>\r\n        &lt;head>\r\n          &lt;title>\u6807\u9898&lt;\/title>\r\n        &lt;\/head>\r\n        &lt;body>\r\n           &lt;div>\r\n             &lt;p>\u6d4b\u8bd5&lt;\/p>\r\n             &lt;p>\u6d4b\u8bd51&lt;\/p>\r\n             &lt;p>\u6d4b\u8bd52&lt;\/p>\r\n             &lt;other>\r\n                 &lt;res>hello,world&lt;\/res>\r\n             &lt;\/other>\r\n           &lt;\/div>\r\n        &lt;\/body>\r\n&lt;\/html>`\r\n\r\/\/\u5b9a\u4e49\u591a\u4e2a\u7ed3\u6784\u4f53,\u53ef\u5c06\u7ed3\u6784\u4f53\u653e\u5728type()\u4e2d\ntype (\r\n        html struct {\r\n                Body body `xml:\"body\"`\r\n        }\r\n        body struct {\r\n                Div div `xml:\"div\"`\r\n        }\r\n        div struct {\r\n                P     &#91;]string `xml:\"p\"`\r\n                Other other    `xml:\"other\"`\r\n        }\r\n        other struct {\r\n                Res string `xml:\"res\"`\r\n        }\r\n)\r\n\r\nfunc main() {\r\n        var html html\r\n        b := &#91;]byte(s)\r\n        err := xml.Unmarshal(b, &amp;html)\r\n        if err != nil {\r\n                log.Fatal(err)\r\n        }\r\n        fmt.Println(\"p\u6807\u7b7e\u5185\u5bb9\u4e3a:\", html.Body.Div.P)\r\n\tfmt.Println(\"res\u6807\u7b7e\u5185\u5bb9\u4e3a:\", html.Body.Div.Other.Res)\r\n}\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u901a\u8fc7const\u5b9a\u4e49\u5e38\u91cf\uff0c\u503c\u4e3axml\u683c\u5f0f\u5185\u5bb9<\/li><li>\u901a\u8fc7type\u5b9a\u4e49\u591a\u4e2a\u7ed3\u6784\u4f53\uff0chtml\u7ed3\u6784\u4f53\u7684\u5143\u7d20\u5b9e\u5219\u4e3a\u7b2c\u4e8c\u4e2a\u7ed3\u6784\u4f53\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u6709\u591a\u4e2a\u6807\u7b7e\u5d4c\u5957\u5c31\u53ef\u5b9a\u4e49\u591a\u4e2a\u7ed3\u6784\u4f53\u5d4c\u5957\uff0c\u6ce8\u610f\u6bcf\u4e2a\u5b57\u6bb5\u540e\u9762\u9700\u8981\u6dfb\u52a0\u7ed3\u6784\u4f53\u6807\u7b7e\uff0c\u4ee5xml\u5f00\u5934\uff0c\u56e0\u4e3adiv\u4e0b\u7684p\u6807\u7b7e\u6709\u591a\u4e2a\uff0c\u56e0\u6b64\u5728\u7ed3\u6784\u4f53\u5b57\u6bb5\u7c7b\u578b\u4f4d\u7f6e\u8981\u5199[]string(\u5b57\u7b26\u4e32\u7c7b\u578b\u5207\u7247)\uff0c\u540e\u9762\u7684res\u53ea\u6709\u4e00\u4e2a\u6807\u7b7e\uff0c\u56e0\u6b64\u7c7b\u578b\u5c31\u4f2astring<\/li><li>\u901a\u8fc7[]byte()\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5207\u7247\uff0c\u7136\u540e\u901a\u8fc7xml.Unmarshal\u53cd\u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4f53html\u4e2d<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"93\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/02\/13.png\" alt=\"\" class=\"wp-image-11334\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/02\/13.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/02\/13-300x39.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/02\/13-600x79.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4e0b\u9762\u4f8b\u5b50\u5c06xml\u6587\u4ef6\u53cd\u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4f53\u4e2d\uff0c\u5982\u4e0b\uff1a \u901a\u8fc7const\u5b9a\u4e49\u5e38\u91cf\uff0c\u503c\u4e3axml\u683c\u5f0f\u5185\u5bb9 \u901a\u8fc7type\u5b9a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":11325,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-11324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/11324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=11324"}],"version-history":[{"count":10,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/11324\/revisions"}],"predecessor-version":[{"id":11336,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/11324\/revisions\/11336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/11325"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=11324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=11324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=11324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}