{"id":275131,"date":"2024-05-16T14:19:59","date_gmt":"2024-05-16T06:19:59","guid":{"rendered":""},"modified":"2024-05-16T14:20:17","modified_gmt":"2024-05-16T06:20:17","slug":"go-%e7%bc%96%e7%a8%8b%e9%a1%b9%e7%9b%ae%e4%b8%ad-xml-%e6%80%8e%e4%b9%88%e8%a7%a3%e6%9e%90%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/275131.html","title":{"rendered":"Go \u7f16\u7a0b\u9879\u76ee\u4e2d XML \u600e\u4e48\u89e3\u6790\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200217\/ecc2a5ab-ccb5-4aa5-be09-e0feb2ac135f.webp\" alt=\"Go \u7f16\u7a0b\u9879\u76ee\u4e2d XML \u600e\u4e48\u89e3\u6790\u6a21\u5757\" \/><\/p>\n<p><p>\u5728Go\u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u89e3\u6790XML\u6d89\u53ca\u5230\u4f7f\u7528Go\u7684\u6807\u51c6\u5e93<code>encoding\/xml<\/code>\u8fdb\u884c\u6570\u636e\u7684\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u3002<strong>\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\u4f7f\u7528<code>xml.Unmarshal<\/code>\u6765\u89e3\u6790XML\u5230Go\u7684\u7ed3\u6784\u4f53\u4e2d\u3001\u4f7f\u7528<code>xml.Marshal<\/code>\u5c06\u7ed3\u6784\u4f53\u5e8f\u5217\u5316\u56deXML<\/strong>\u3002\u8fd9\u4e9b\u64cd\u4f5c\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u5728Go\u8bed\u8a00\u9879\u76ee\u4e2d\u8f7b\u677e\u5730\u5904\u7406XML\u6570\u636e\uff0c\u65e0\u8bba\u662f\u4eceXML\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u8fd8\u662f\u5c06\u6570\u636e\u8f93\u51fa\u4e3aXML\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>xml.Unmarshal<\/code>\u6765\u89e3\u6790XML\u5230Go\u7ed3\u6784\u4f53\u4e2d<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u4e0eXML\u6587\u6863\u7ed3\u6784\u76f8\u5bf9\u5e94\u7684Go\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u901a\u8fc7<code>xml.Unmarshal<\/code>\u51fd\u6570\u76f4\u63a5\u5c06XML\u6587\u6863\u4e2d\u7684\u6570\u636e\u8bfb\u53d6\u5230\u8fd9\u4e9b\u7ed3\u6784\u4f53\u5b9e\u4f8b\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4fbf\u5229\u6027\u5728\u4e8e\u5b83\u62bd\u8c61\u4e86XML\u7684\u89e3\u6790\u8fc7\u7a0b\uff0c\u5f00\u53d1\u8005\u65e0\u9700\u5173\u6ce8XML\u7684\u5177\u4f53\u89e3\u6790\u903b\u8f91\uff0c\u53ea\u9700\u5173\u6ce8\u5982\u4f55\u4ece\u8fd9\u4e9b\u7ed3\u6784\u4f53\u5b9e\u4f8b\u4e2d\u8bbf\u95ee\u6570\u636e\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u89e3\u6790XML\u5230Go\u7ed3\u6784\u4f53<\/h2>\n<\/p>\n<p><p>\u5728\u89e3\u6790XML\u6570\u636e\u5230Go\u7ed3\u6784\u4f53\u65f6\uff0c\u9996\u5148\u8981\u5b9a\u4e49\u4e00\u4e2a\u6216\u591a\u4e2aGo\u7ed3\u6784\u4f53\uff0c\u8fd9\u4e9b\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u5bf9\u5e94\u4e8eXML\u6570\u636e\u4e2d\u7684\u6807\u7b7e\u3002\u901a\u8fc7\u5728\u7ed3\u6784\u4f53\u5b57\u6bb5\u4e0a\u4f7f\u7528<code>xml<\/code>\u6807\u7b7e\u8fdb\u884c\u6ce8\u91ca\uff0c\u53ef\u4ee5\u63a7\u5236\u4eceXML\u6587\u4ef6\u5230\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u6620\u5c04\u3002<\/p>\n<\/p>\n<p><h3>\u51c6\u5907XML\u6570\u636e\u548cGo\u7ed3\u6784\u4f53<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7ed9\u51fa\u4e00\u4e2aXML\u793a\u4f8b\u548c\u5bf9\u5e94\u7684Go\u7ed3\u6784\u4f53\u5b9a\u4e49\u3002\u5047\u8bbe\u6709\u5982\u4e0b\u7684XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-xml\">&lt;person&gt;<\/p>\n<p>    &lt;name&gt;John Doe&lt;\/name&gt;<\/p>\n<p>    &lt;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&gt;john.doe@example.com&lt;\/email&gt;<\/p>\n<p>&lt;\/person&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u5e94\u7684Go\u7ed3\u6784\u4f53\u53ef\u80fd\u5982\u4e0b\u6240\u5b9a\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Person struct {<\/p>\n<p>    Name  string `xml:&quot;name&quot;`<\/p>\n<p>    Email string `xml:&quot;email&quot;`<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528xml.Unmarshal\u8fdb\u884c\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u89e3\u6790XML\u6587\u4ef6\u5230\u7ed3\u6784\u4f53\u7684\u8fc7\u7a0b\u975e\u5e38\u76f4\u63a5\uff0c\u53ea\u9700\u8981\u8c03\u7528<code>xml.Unmarshal<\/code>\u51fd\u6570\uff0c\u5e76\u4f20\u5165XML\u6570\u636e\u548c\u76ee\u6807\u7ed3\u6784\u4f53\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">func main() {<\/p>\n<p>    data := []byte(`&lt;person&gt;&lt;name&gt;John Doe&lt;\/name&gt;&lt;email&gt;john.doe@example.com&lt;\/email&gt;&lt;\/person&gt;`)<\/p>\n<p>    var person Person<\/p>\n<p>    err := xml.Unmarshal(data, &amp;person)<\/p>\n<p>    if err != nil {<\/p>\n<p>        log.Fatalf(&quot;xml.Unmarshal failed with &#039;%s&#039;\\n&quot;, err)<\/p>\n<p>    }<\/p>\n<p>    fmt.Printf(&quot;Name: %s, Email: %s\\n&quot;, person.Name, person.Email)<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u6bb5\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542bXML\u6570\u636e\u7684\u5b57\u8282\u5207\u7247\u3002\u7136\u540e\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2aPerson\u7c7b\u578b\u7684\u53d8\u91cf<code>person<\/code>\uff0c\u5e76\u5c06\u5176\u5730\u5740\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u7ed9<code>xml.Unmarshal<\/code>\u51fd\u6570\u3002\u5982\u679c\u89e3\u6790\u6210\u529f\uff0c\u7ed3\u6784\u4f53<code>person<\/code>\u5c06\u88ab\u586b\u5145\u76f8\u5e94\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5c06Go\u7ed3\u6784\u4f53\u5e8f\u5217\u5316\u4e3aXML<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u62e5\u6709\u4e86\u586b\u5145\u4e86\u6570\u636e\u7684Go\u7ed3\u6784\u4f53\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\u8981\u5c06\u5176\u8f6c\u6362\u56deXML\u683c\u5f0f\u3002Go\u901a\u8fc7<code>xml.Marshal<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u5b9a\u4e49Go\u7ed3\u6784\u4f53<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0e\u4e0a\u9762\u76f8\u540c\u7684Person\u7ed3\u6784\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Person struct {<\/p>\n<p>    Name  string `xml:&quot;name&quot;`<\/p>\n<p>    Email string `xml:&quot;email&quot;`<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528xml.Marshal\u8fdb\u884c\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><p>\u5c06\u7ed3\u6784\u4f53\u5e8f\u5217\u5316\u4e3aXML\u7684\u8fc7\u7a0b\u540c\u6837\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">func main() {<\/p>\n<p>    person := Person{Name: &quot;John Doe&quot;, Email: &quot;john.doe@example.com&quot;}<\/p>\n<p>    output, err := xml.MarshalIndent(person, &quot;&quot;, &quot;    &quot;)<\/p>\n<p>    if err != nil {<\/p>\n<p>        log.Fatalf(&quot;xml.Marshal failed with &#039;%s&#039;\\n&quot;, err)<\/p>\n<p>    }<\/p>\n<p>    fmt.Printf(&quot;%s\\n&quot;, output)<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>xml.MarshalIndent<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u4e0d\u4ec5\u80fd\u591f\u5c06\u6570\u636e\u5e8f\u5217\u5316\u4e3aXML\uff0c\u8fd8\u80fd\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u9605\u8bfb\u3002\u5982\u679c\u4f60\u4e0d\u9700\u8981\u7f29\u8fdb\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>xml.Marshal<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5904\u7406XML\u5c5e\u6027\u548c\u5d4c\u5957\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u590d\u6742\u7684XML\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u89e3\u6790XML\u5c5e\u6027\u6216\u5d4c\u5957\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u89e3\u6790XML\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5982\u679cXML\u5143\u7d20\u5177\u6709\u5c5e\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728Go\u7ed3\u6784\u4f53\u4e2d\u5b9a\u4e49\u9002\u5f53\u7684\u5b57\u6bb5\u6765\u89e3\u6790\u8fd9\u4e9b\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Person struct {<\/p>\n<p>    Name  string `xml:&quot;name&quot;`<\/p>\n<p>    Email string `xml:&quot;email,attr&quot;`<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u901a\u8fc7\u5728<code>xml<\/code>\u6807\u7b7e\u4e2d\u4f7f\u7528<code>attr<\/code>\u6307\u793a\u7b26\uff0c\u53ef\u4ee5\u6307\u660e\u76f8\u5e94\u7684\u5b57\u6bb5\u5e94\u8be5\u4e0e\u4e00\u4e2aXML\u5c5e\u6027\u7ed1\u5b9a\uff0c\u800c\u975e\u5b50\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u89e3\u6790\u5d4c\u5957XML\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5d4c\u5957\u5143\u7d20\u65f6\uff0c\u53ea\u9700\u5728\u7236\u7ed3\u6784\u4f53\u4e2d\u5d4c\u5165\u53e6\u4e00\u4e2a\u7ed3\u6784\u4f53\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Address struct {<\/p>\n<p>    City    string `xml:&quot;city&quot;`<\/p>\n<p>    Country string `xml:&quot;country&quot;`<\/p>\n<p>}<\/p>\n<p>type Person struct {<\/p>\n<p>    Name    string  `xml:&quot;name&quot;`<\/p>\n<p>    Email   string  `xml:&quot;email&quot;`<\/p>\n<p>    Address Address `xml:&quot;address&quot;`<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684XML\u6570\u636e\u7ed3\u6784\uff0c\u5305\u62ec\u90a3\u4e9b\u5177\u6709\u5d4c\u5957\u5143\u7d20\u7684XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u5904\u7406XML\u65f6\uff0c\u6b63\u786e\u7684\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>xml.Unmarshal<\/code>\u548c<code>xml.Marshal<\/code>\u65f6\uff0c\u91cd\u8981\u7684\u662f\u8981\u68c0\u67e5\u8fd4\u56de\u7684\u9519\u8bef\u3002\u8fd9\u4e9b\u9519\u8bef\u53ef\u4ee5\u63d0\u4f9b\u89e3\u6790\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u95ee\u9898\u7684\u5177\u4f53\u4fe1\u606f\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u5730\u5b9a\u4f4d\u5e76\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u8c03\u8bd5\u6280\u5de7<\/h3>\n<\/p>\n<ul>\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u5728\u89e3\u6790\u6216\u5e8f\u5217\u5316\u671f\u95f4\u8bb0\u5f55\u5173\u952e\u4fe1\u606f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7406\u89e3\u6570\u636e\u7684\u6d41\u52a8\u3002<\/li>\n<li><strong>\u4f7f\u7528XML\u9a8c\u8bc1\u5de5\u5177<\/strong>\uff1a\u5728\u5904\u7406\u590d\u6742\u7684XML\u7ed3\u6784\u65f6\uff0c\u4f7f\u7528\u5728\u7ebfXML\u9a8c\u8bc1\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd\u4f60\u7684XML\u6570\u636e\u683c\u5f0f\u6b63\u786e\u3002<\/li>\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong>\uff1a\u4e3a\u89e3\u6790\u548c\u5e8f\u5217\u5316\u903b\u8f91\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u786e\u4fdd\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5904\u7406\u9884\u671f\u7684\u8f93\u5165\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u7b56\u7565\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5728Go\u9879\u76ee\u4e2d\u89e3\u6790\u548c\u5904\u7406XML\u6570\u636e\uff0c\u63d0\u9ad8\u9879\u76ee\u7684\u8d28\u91cf\u548c\u5065\u58f2\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 Go \u7f16\u7a0b\u9879\u76ee\u4e2d\u89e3\u6790 XML\uff1f<\/strong><br \/>\nGo \u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 <code>encoding\/xml<\/code> \u5305\u6765\u89e3\u6790 XML \u6570\u636e\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a struct \u6765\u8868\u793a XML \u6570\u636e\u7684\u7ed3\u6784\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>xml.Unmarshal()<\/code> \u51fd\u6570\u5c06 XML \u6570\u636e\u89e3\u6790\u4e3a Go \u5bf9\u8c61\u3002\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee Go \u5bf9\u8c61\u7684\u5b57\u6bb5\u6765\u83b7\u53d6 XML \u6570\u636e\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-go\">type Person struct {\n    Name  string `xml:&quot;name&quot;`\n    Age   int    `xml:&quot;age&quot;`\n    Email string `xml:&quot;email&quot;`\n}\n\nfunc main() {\n    xmlData := []byte(`\n        &lt;person&gt;\n            &lt;name&gt;John&lt;\/name&gt;\n            &lt;age&gt;30&lt;\/age&gt;\n            &lt;email&gt;john@example.com&lt;\/email&gt;\n        &lt;\/person&gt;\n    `)\n\n    var p Person\n    if err := xml.Unmarshal(xmlData, &amp;p); err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Println(&quot;Name:&quot;, p.Name)\n    fmt.Println(&quot;Age:&quot;, p.Age)\n    fmt.Println(&quot;Email:&quot;, p.Email)\n}\n<\/code><\/pre>\n<p><strong>2. \u5728 Go \u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u5904\u7406 XML \u4e2d\u7684\u5d4c\u5957\u5143\u7d20\uff1f<\/strong><br \/>\n\u5728 Go \u7f16\u7a0b\u9879\u76ee\u4e2d\u5904\u7406 XML \u4e2d\u7684\u5d4c\u5957\u5143\u7d20\u5f88\u7b80\u5355\u3002\u4f60\u53ea\u9700\u8981\u5728\u5b9a\u4e49 struct \u7684\u5b57\u6bb5\u4e0a\u4f7f\u7528 <code>xml<\/code> \u6807\u7b7e\uff0c\u6307\u5b9a XML \u5143\u7d20\u540d\u79f0\u5373\u53ef\u3002\u5982\u679c XML \u5143\u7d20\u662f\u5d4c\u5957\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>parent&gt;child<\/code> \u683c\u5f0f\u8868\u793a\u5d4c\u5957\u5173\u7cfb\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-go\">type Book struct {\n    Title  string `xml:&quot;title&quot;`\n    Author string `xml:&quot;author&quot;`\n    Year   int    `xml:&quot;details&gt;year&quot;`\n}\n\nfunc main() {\n    xmlData := []byte(`\n        &lt;book&gt;\n            &lt;title&gt;The Great Gatsby&lt;\/title&gt;\n            &lt;author&gt;F. Scott Fitzgerald&lt;\/author&gt;\n            &lt;details&gt;\n                &lt;year&gt;1925&lt;\/year&gt;\n            &lt;\/details&gt;\n        &lt;\/book&gt;\n    `)\n\n    var b Book\n    if err := xml.Unmarshal(xmlData, &amp;b); err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Println(&quot;Title:&quot;, b.Title)\n    fmt.Println(&quot;Author:&quot;, b.Author)\n    fmt.Println(&quot;Year:&quot;, b.Year)\n}\n<\/code><\/pre>\n<p><strong>3. \u5982\u4f55\u5728 Go \u7f16\u7a0b\u9879\u76ee\u4e2d\u5904\u7406 XML \u4e2d\u7684\u5c5e\u6027\uff1f<\/strong><br \/>\n\u8981\u5728 Go \u7f16\u7a0b\u9879\u76ee\u4e2d\u5904\u7406 XML \u4e2d\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728 struct \u7684\u5b57\u6bb5\u4e0a\u4f7f\u7528 <code>xml<\/code> \u6807\u7b7e\uff0c\u6307\u5b9a\u5c5e\u6027\u540d\u79f0\u3002\u5728 XML \u4e2d\uff0c\u5c5e\u6027\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u5143\u7d20\u7684\u5c5e\u6027\u540d\u548c\u5c5e\u6027\u503c\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-go\">type Person struct {\n    Name  string `xml:&quot;name,attr&quot;`\n    Age   int    `xml:&quot;age,attr&quot;`\n    Email string `xml:&quot;email,attr&quot;`\n}\n\nfunc main() {\n    xmlData := []byte(`\n        &lt;person name=&quot;John&quot; age=&quot;30&quot; email=&quot;john@example.com&quot; \/&gt;\n    `)\n\n    var p Person\n    if err := xml.Unmarshal(xmlData, &amp;p); err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Println(&quot;Name:&quot;, p.Name)\n    fmt.Println(&quot;Age:&quot;, p.Age)\n    fmt.Println(&quot;Email:&quot;, p.Email)\n}\n<\/code><\/pre>\n<p>\u901a\u8fc7\u5728\u5b57\u6bb5\u4e0a\u4f7f\u7528 <code>attr<\/code> \u6807\u7b7e\uff0c\u53ef\u4ee5\u5c06 XML \u5143\u7d20\u7684\u5c5e\u6027\u4e0e Go struct \u4e2d\u7684\u5b57\u6bb5\u5173\u8054\u8d77\u6765\uff0c\u4ee5\u4fbf\u5728\u89e3\u6790 XML \u65f6\u6b63\u786e\u83b7\u53d6\u5c5e\u6027\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Go\u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u89e3\u6790XML\u6d89\u53ca\u5230\u4f7f\u7528Go\u7684\u6807\u51c6\u5e93encoding\/xml\u8fdb\u884c\u6570\u636e\u7684\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u3002\u6838\u5fc3\u89c2\u70b9 [&hellip;]","protected":false},"author":3,"featured_media":275155,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275131"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=275131"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/275155"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=275131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=275131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=275131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}