{"id":57648,"date":"2024-10-10T19:59:19","date_gmt":"2024-10-10T11:59:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57648\/"},"modified":"2024-10-10T19:59:19","modified_gmt":"2024-10-10T11:59:19","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-xml-%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%af%ab%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57648\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00 XML \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-xml-%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%af%ab%e6%93%8d%e4%bd%9c-JvTyvBYfNV\">GO\u6559\u7a0b: Go \u8a9e\u8a00 XML \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0cXML\uff08\u53ef\u64f4\u5c55\u6a19\u8a18\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u6578\u64da\u683c\u5f0f\uff0c\u7279\u5225\u662f\u5728\u6578\u64da\u4ea4\u63db\u548c\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002Go \u8a9e\u8a00\uff08\u53c8\u7a31 Golang\uff09\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6a19\u6e96\u5eab\u4f86\u8655\u7406 XML \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u9032\u884c XML \u6587\u4ef6\u7684\u8b80\u53d6\u548c\u5beb\u5165\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u7bc4\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"xml-%e6%96%87%e4%bb%b6%e7%9a%84%e7%b5%90%e6%a7%8b-JvTyvBYfNV\">XML \u6587\u4ef6\u7684\u7d50\u69cb<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u4e86\u89e3 XML \u6587\u4ef6\u7684\u57fa\u672c\u7d50\u69cb\u3002XML \u6587\u4ef6\u7531\u6a19\u7c64\u7d44\u6210\uff0c\u9019\u4e9b\u6a19\u7c64\u53ef\u4ee5\u5d4c\u5957\uff0c\u4e26\u4e14\u53ef\u4ee5\u5305\u542b\u5c6c\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 XML \u6587\u4ef6\u7bc4\u4f8b\uff1a<\/p>\n<pre>\n<code>\n\n\n    \n        <title>Go \u8a9e\u8a00\u5165\u9580<\/title>\n        \u5f35\u4e09\n        2021\n    \n    \n        <title>\u6df1\u5165\u7406\u89e3 Go<\/title>\n        \u674e\u56db\n        2022\n    \n\n<\/code>\n<\/pre>\n<h2 id=\"%e8%ae%80%e5%8f%96-xml-%e6%96%87%e4%bb%b6-JvTyvBYfNV\">\u8b80\u53d6 XML \u6587\u4ef6<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u6a19\u6e96\u5eab\u4e2d\u7684 <code>encoding\/xml<\/code> \u5305\u4f86\u8b80\u53d6 XML \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u8b80\u53d6 XML \u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5b9a\u7fa9\u7d50\u69cb\u9ad4\u4ee5\u6620\u5c04 XML \u6a19\u7c64\u3002<\/li>\n<li>\u4f7f\u7528 <code>xml.Unmarshal<\/code> \u51fd\u6578\u5c07 XML \u6578\u64da\u89e3\u6790\u5230\u7d50\u69cb\u9ad4\u4e2d\u3002<\/li>\n<\/ol>\n<h3 id=\"%e7%af%84%e4%be%8b%e4%bb%a3%e7%a2%bc-JvTyvBYfNV\">\u7bc4\u4f8b\u4ee3\u78bc<\/h3>\n<pre>\n<code>\npackage main\n\nimport (\n    \"encoding\/xml\"\n    \"fmt\"\n    \"os\"\n)\n\ntype Book struct {\n    ID     string `xml:\"id,attr\"`\n    Title  string `xml:\"title\"`\n    Author string `xml:\"author\"`\n    Year   string `xml:\"year\"`\n}\n\ntype Books struct {\n    BookList []Book `xml:\"book\"`\n}\n\nfunc main() {\n    file, err := os.Open(\"books.xml\")\n    if err != nil {\n        fmt.Println(\"Error opening file:\", err)\n        return\n    }\n    defer file.Close()\n\n    var books Books\n    decoder := xml.NewDecoder(file)\n    err = decoder.Decode(&amp;books)\n    if err != nil {\n        fmt.Println(\"Error decoding XML:\", err)\n        return\n    }\n\n    for _, book := range books.BookList {\n        fmt.Printf(\"ID: %s, Title: %s, Author: %s, Year: %sn\", book.ID, book.Title, book.Author, book.Year)\n    }\n}\n<\/code>\n<\/pre>\n<h2 id=\"%e5%af%ab%e5%85%a5-xml-%e6%96%87%e4%bb%b6-JvTyvBYfNV\">\u5beb\u5165 XML \u6587\u4ef6<\/h2>\n<p>\u9664\u4e86\u8b80\u53d6 XML \u6587\u4ef6\uff0cGo \u8a9e\u8a00\u4e5f\u652f\u6301\u5c07\u6578\u64da\u5beb\u5165 XML \u6587\u4ef6\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4f7f\u7528 <code>xml.Marshal<\/code> \u51fd\u6578\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u5beb\u5165 XML \u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5b9a\u7fa9\u7d50\u69cb\u9ad4\u4ee5\u8868\u793a\u8981\u5beb\u5165\u7684\u6578\u64da\u3002<\/li>\n<li>\u4f7f\u7528 <code>xml.MarshalIndent<\/code> \u51fd\u6578\u5c07\u7d50\u69cb\u9ad4\u8f49\u63db\u70ba XML \u683c\u5f0f\u3002<\/li>\n<li>\u5c07\u751f\u6210\u7684 XML \u6578\u64da\u5beb\u5165\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<h3 id=\"%e7%af%84%e4%be%8b%e4%bb%a3%e7%a2%bc-JvTyvBYfNV\">\u7bc4\u4f8b\u4ee3\u78bc<\/h3>\n<pre>\n<code>\npackage main\n\nimport (\n    \"encoding\/xml\"\n    \"fmt\"\n    \"os\"\n)\n\ntype Book struct {\n    ID     string `xml:\"id,attr\"`\n    Title  string `xml:\"title\"`\n    Author string `xml:\"author\"`\n    Year   string `xml:\"year\"`\n}\n\ntype Books struct {\n    BookList []Book `xml:\"book\"`\n}\n\nfunc main() {\n    books := Books{\n        BookList: []Book{\n            {ID: \"1\", Title: \"Go \u8a9e\u8a00\u5165\u9580\", Author: \"\u5f35\u4e09\", Year: \"2021\"},\n            {ID: \"2\", Title: \"\u6df1\u5165\u7406\u89e3 Go\", Author: \"\u674e\u56db\", Year: \"2022\"},\n        },\n    }\n\n    output, err := xml.MarshalIndent(books, \"\", \"  \")\n    if err != nil {\n        fmt.Println(\"Error marshaling XML:\", err)\n        return\n    }\n\n    output = append([]byte(xml.Header), output...)\n\n    err = os.WriteFile(\"output.xml\", output, 0644)\n    if err != nil {\n        fmt.Println(\"Error writing file:\", err)\n        return\n    }\n\n    fmt.Println(\"XML file written successfully.\")\n}\n<\/code>\n<\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-JvTyvBYfNV\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u9032\u884c XML \u6587\u4ef6\u7684\u8b80\u53d6\u548c\u5beb\u5165\u64cd\u4f5c\u3002\u901a\u904e\u4f7f\u7528 <code>encoding\/xml<\/code> \u5305\uff0c\u6211\u5011\u80fd\u5920\u8f15\u9b06\u5730\u5c07 XML \u6578\u64da\u6620\u5c04\u5230\u7d50\u69cb\u9ad4\uff0c\u4e26\u5c07\u7d50\u69cb\u9ad4\u8f49\u63db\u70ba XML \u683c\u5f0f\u3002\u9019\u4e9b\u64cd\u4f5c\u5728\u8a31\u591a\u61c9\u7528\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u8207\u5176\u4ed6\u7cfb\u7d71\u9032\u884c\u6578\u64da\u4ea4\u63db\u6642\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 Go \u61c9\u7528\u7a0b\u5f0f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u8b80\u53d6\u548c\u5beb\u5165 XML \u6587\u4ef6\uff0c\u638c\u63e1\u57fa\u672c\u64cd\u4f5c\u548c\u5be6\u7528\u7bc4\u4f8b\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57648","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57648"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57648\/revisions"}],"predecessor-version":[{"id":57649,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57648\/revisions\/57649"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}