{"id":41828,"date":"2024-12-01T11:07:03","date_gmt":"2024-12-01T03:07:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41828\/"},"modified":"2024-12-01T11:07:03","modified_gmt":"2024-12-01T03:07:03","slug":"xml-%e8%a7%a3%e7%bb%84%e5%b1%9e%e6%80%a7%e5%92%8c%e5%86%85%e9%83%a8%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41828\/","title":{"rendered":"XML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\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>XML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c<\/span><\/p>\n<h1>XML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 17:18:25<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aXML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5f53\u5c1d\u8bd5\u89e3\u6790\u67d0\u4e9b xml \u65f6\uff0c\u5982\u4e0b\u9762\u7684\u793a\u4f8b\u6240\u793a\u3002<\/p>\n<p>\u5982\u4f55\u5f15\u7528\u548c\u89e3\u6790owner\u7684\u5c5e\u6027\u201cid\u201d\uff1f<\/p>\n<pre>import (\n    \"encoding\/xml\"\n    \"fmt\"\n)\n\nvar Sample = `&lt;?xml version=\"1.0\"?&gt;                                                                                                                                                                      \n&lt;items&gt;                                                                                                                                                                                     \n  &lt;item id=\"10\"&gt;                                                                                                                                                                            \n    &lt;owner id=\"50\"&gt;John Smith&lt;\/owner&gt;                                                                                                                                                       \n    &lt;name&gt;Box&lt;\/name&gt;                                                                                                                                                                        \n    &lt;location&gt;Kitchen&lt;\/location&gt;                                                                                                                                                            \n  &lt;\/item&gt;                                                                                                                                                                                   \n&lt;\/items&gt;`\n\ntype Item struct {\n    XMLName  xml.Name `xml:\"item\"`\n    Id       int      `xml:\"id,attr\"`\n    Owner    string   `xml:\"owner\"`\n    OwnerId  int      `xml:\"owner,id,attr\"`\n    Name     string   `xml:\"name\"`\n    Location string   `xml:\"location\"`\n}\n\ntype Items struct {\n    XMLName xml.Name `xml:\"items\"`\n    Items   []Item   `xml:\"item\"`\n}\n\nfunc main() {\n    items := Items{}\n    data := []byte(Sample)\n    xml.Unmarshal(data, &amp;items)\n    fmt.Println(\"items: \", items)\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f15\u7528\u6240\u6709\u8005\u7684\u5c5e\u6027\u201cid\u201d\uff1a<code>items.items[0].owner.id<\/code><\/p>\n<pre>fmt.Println(\"Owner Id: \", items.Items[0].Owner.Id)<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aXML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; XML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c XML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c \u6765\u6e90\uff1astackoverflow 2024-04-21 17:18:25 0\u6d4f\u89c8 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300aXML \u89e3\u7ec4\u5c5e\u6027\u548c\u5185\u90e8\u503c\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u5f53\u5c1d\u8bd5\u89e3\u6790\u67d0\u4e9b xml \u65f6\uff0c\u5982\u4e0b\u9762\u7684\u793a\u4f8b\u6240\u793a\u3002 \u5982\u4f55\u5f15\u7528\u548c\u89e3\u6790owner\u7684\u5c5e\u6027\u201cid\u201d\uff1f import ( &#8220;encoding\/xml&#8221; &#8220;fmt&#8221; ) var Sample = `&lt;?xml version=&#8221;1.0&#8243;?&gt; &lt;items&gt; &lt;item id=&#8221;10&#8243;&gt; &lt;owner id=&#8221;50&#8243;&gt;John Smith&lt;\/owner&gt; &lt;name&gt;Box&lt;\/name&gt; &lt;location&gt;Kitchen&lt;\/location&gt; &lt;\/item&gt; &lt;\/items&gt;` type Item struct { XMLName xml.Name `xml:&#8221;item&#8221;` Id int `xml:&#8221;id,attr&#8221;` Owner string `xml:&#8221;owner&#8221;` OwnerId [&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-41828","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41828","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=41828"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41828\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}