forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.html
274 lines (218 loc) · 14.8 KB
/
xml.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: XML</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'json';
}
if (e.key == "ArrowRight") {
window.location.href = 'time';
}
}
</script>
<body>
<div class="example" id="xml">
<h2><a href="./">Go by Example 中文版</a>: XML</h2>
<table>
<tr>
<td class="docs">
<p>Go 通过 <code>encoding.xml</code> 包为 XML 和 类-XML 格式提供了内建支持。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/IcyhVDXu1wi"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"encoding/xml"</span>
<span class="s">"fmt"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Plant</code> 结构将被映射到 <code>XML</code> 。
与 <code>JSON</code> 示例类似,字段标签包含用于编码器和解码器的指令。
这里我们使用了 <code>XML</code> 包的一些特性:
<code>XMLName</code> 字段名称规定了该结构的 <code>XML</code> 元素名称;
<code>id,attrr</code> 表示 <code>Id</code> 字段是一个 <code>XML</code> 属性,而不是嵌套元素。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">type</span> <span class="nx">Plant</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">XMLName</span> <span class="nx">xml</span><span class="p">.</span><span class="nx">Name</span> <span class="s">`xml:"plant"`</span>
<span class="nx">Id</span> <span class="kt">int</span> <span class="s">`xml:"id,attr"`</span>
<span class="nx">Name</span> <span class="kt">string</span> <span class="s">`xml:"name"`</span>
<span class="nx">Origin</span> <span class="p">[]</span><span class="kt">string</span> <span class="s">`xml:"origin"`</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="p">(</span><span class="nx">p</span> <span class="nx">Plant</span><span class="p">)</span> <span class="nf">String</span><span class="p">()</span> <span class="kt">string</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Sprintf</span><span class="p">(</span><span class="s">"Plant id=%v, name=%v, origin=%v"</span><span class="p">,</span>
<span class="nx">p</span><span class="p">.</span><span class="nx">Id</span><span class="p">,</span> <span class="nx">p</span><span class="p">.</span><span class="nx">Name</span><span class="p">,</span> <span class="nx">p</span><span class="p">.</span><span class="nx">Origin</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">coffee</span> <span class="o">:=</span> <span class="o">&</span><span class="nx">Plant</span><span class="p">{</span><span class="nx">Id</span><span class="p">:</span> <span class="mi">27</span><span class="p">,</span> <span class="nx">Name</span><span class="p">:</span> <span class="s">"Coffee"</span><span class="p">}</span>
<span class="nx">coffee</span><span class="p">.</span><span class="nx">Origin</span> <span class="p">=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">"Ethiopia"</span><span class="p">,</span> <span class="s">"Brazil"</span><span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>传入我们声明了 XML 的 Plant 类型。
使用 <code>MarshalIndent</code> 生成可读性更好的输出结果。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">out</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">xml</span><span class="p">.</span><span class="nf">MarshalIndent</span><span class="p">(</span><span class="nx">coffee</span><span class="p">,</span> <span class="s">" "</span><span class="p">,</span> <span class="s">" "</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">out</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>明确的为输出结果添加一个通用的 XML 头部信息。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">xml</span><span class="p">.</span><span class="nx">Header</span> <span class="o">+</span> <span class="nb">string</span><span class="p">(</span><span class="nx">out</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>使用 <code>Unmarshal</code> 将 <code>XML</code> 格式字节流解析到 <code>Plant</code> 结构。
如果 <code>XML</code> 格式错误或无法映射到 <code>Plant</code> 结构,将返回一个描述性错误。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">var</span> <span class="nx">p</span> <span class="nx">Plant</span>
<span class="k">if</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">xml</span><span class="p">.</span><span class="nf">Unmarshal</span><span class="p">(</span><span class="nx">out</span><span class="p">,</span> <span class="o">&</span><span class="nx">p</span><span class="p">);</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">p</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">tomato</span> <span class="o">:=</span> <span class="o">&</span><span class="nx">Plant</span><span class="p">{</span><span class="nx">Id</span><span class="p">:</span> <span class="mi">81</span><span class="p">,</span> <span class="nx">Name</span><span class="p">:</span> <span class="s">"Tomato"</span><span class="p">}</span>
<span class="nx">tomato</span><span class="p">.</span><span class="nx">Origin</span> <span class="p">=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">"Mexico"</span><span class="p">,</span> <span class="s">"California"</span><span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>parent>child>plant</code> 字段标签告诉编码器,将 <code>Plants</code> 中的元素嵌套在 <code><parent><child></code> 里面。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">type</span> <span class="nx">Nesting</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">XMLName</span> <span class="nx">xml</span><span class="p">.</span><span class="nx">Name</span> <span class="s">`xml:"nesting"`</span>
<span class="nx">Plants</span> <span class="p">[]</span><span class="o">*</span><span class="nx">Plant</span> <span class="s">`xml:"parent>child>plant"`</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">nesting</span> <span class="o">:=</span> <span class="o">&</span><span class="nx">Nesting</span><span class="p">{}</span>
<span class="nx">nesting</span><span class="p">.</span><span class="nx">Plants</span> <span class="p">=</span> <span class="p">[]</span><span class="o">*</span><span class="nx">Plant</span><span class="p">{</span><span class="nx">coffee</span><span class="p">,</span> <span class="nx">tomato</span><span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"> <span class="nx">out</span><span class="p">,</span> <span class="nx">_</span> <span class="p">=</span> <span class="nx">xml</span><span class="p">.</span><span class="nf">MarshalIndent</span><span class="p">(</span><span class="nx">nesting</span><span class="p">,</span> <span class="s">" "</span><span class="p">,</span> <span class="s">" "</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">out</span><span class="p">))</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="gp">$</span> go run xml.go
<span class="go"> <plant id="27">
</span><span class="go"> <name>Coffee</name>
</span><span class="go"> <origin>Ethiopia</origin>
</span><span class="go"> <origin>Brazil</origin>
</span><span class="go"> </plant>
</span><span class="go"><?xml version="1.0" encoding="UTF-8"?>
</span><span class="go"> <plant id="27">
</span><span class="go"> <name>Coffee</name>
</span><span class="go"> <origin>Ethiopia</origin>
</span><span class="go"> <origin>Brazil</origin>
</span><span class="go"> </plant>
</span><span class="go">Plant id=27, name=Coffee, origin=[Ethiopia Brazil]
</span><span class="go"> <nesting>
</span><span class="go"> <parent>
</span><span class="go"> <child>
</span><span class="go"> <plant id="27">
</span><span class="go"> <name>Coffee</name>
</span><span class="go"> <origin>Ethiopia</origin>
</span><span class="go"> <origin>Brazil</origin>
</span><span class="go"> </plant>
</span><span class="go"> <plant id="81">
</span><span class="go"> <name>Tomato</name>
</span><span class="go"> <origin>Mexico</origin>
</span><span class="go"> <origin>California</origin>
</span><span class="go"> </plant>
</span><span class="go"> </child>
</span><span class="go"> </parent>
</span><span class="go"> </nesting></span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="time.html">时间</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"encoding/xml\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('type Plant struct {\u000A XMLName xml.Name `xml:\"plant\"`\u000A Id int `xml:\"id,attr\"`\u000A Name string `xml:\"name\"`\u000A Origin []string `xml:\"origin\"`\u000A}\u000A');codeLines.push('func (p Plant) String() string {\u000A return fmt.Sprintf(\"Plant id\u003D%v, name\u003D%v, origin\u003D%v\",\u000A p.Id, p.Name, p.Origin)\u000A}\u000A');codeLines.push('func main() {\u000A coffee :\u003D \u0026Plant{Id: 27, Name: \"Coffee\"}\u000A coffee.Origin \u003D []string{\"Ethiopia\", \"Brazil\"}\u000A');codeLines.push(' out, _ :\u003D xml.MarshalIndent(coffee, \" \", \" \")\u000A fmt.Println(string(out))\u000A');codeLines.push(' fmt.Println(xml.Header + string(out))\u000A');codeLines.push(' var p Plant\u000A if err :\u003D xml.Unmarshal(out, \u0026p); err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(p)\u000A');codeLines.push(' tomato :\u003D \u0026Plant{Id: 81, Name: \"Tomato\"}\u000A tomato.Origin \u003D []string{\"Mexico\", \"California\"}\u000A');codeLines.push(' type Nesting struct {\u000A XMLName xml.Name `xml:\"nesting\"`\u000A Plants []*Plant `xml:\"parent\u003Echild\u003Eplant\"`\u000A }\u000A');codeLines.push(' nesting :\u003D \u0026Nesting{}\u000A nesting.Plants \u003D []*Plant{coffee, tomato}\u000A');codeLines.push(' out, _ \u003D xml.MarshalIndent(nesting, \" \", \" \")\u000A fmt.Println(string(out))\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>