You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/eBook/06.3.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -115,7 +115,7 @@ We introduced a simple session manager's working principles in the previous sect
115
115
}
116
116
117
117
118
-
The above example implemented a memory based session storage mechanism. It uses its `init()` function to register this storage engine to the session manager. So how to register this engine from our main program?
118
+
The above example implemented a memory based session storage mechanism. It uses its `init()` function to register this storage engine to the session manager. So how do we register this engine from our main program?
119
119
120
120
import (
121
121
"github.com/astaxie/session"
@@ -136,4 +136,4 @@ We use the blank import mechanism (which will invoke the package's `init()` func
136
136
137
137
-[Directory](preface.md)
138
138
- Previous section: [How to use sessions in Go](06.2.md)
139
-
- Next section: [Prevent hijack of session](06.4.md)
139
+
- Next section: [Prevent session hijacking](06.4.md)
Copy file name to clipboardExpand all lines: en/eBook/07.1.md
+44-44Lines changed: 44 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# 7.1 XML
2
2
3
-
XML is a commonly used data communication format in web services today, it becomes more and more important role in daily development. In this section, we're going to introduce how to work with XML through standard library.
3
+
XML is a commonly used data communication format in web services. Today, it's assuming a more and more important role in web development. In this section, we're going to introduce how to work with XML through Go's standard library.
4
4
5
-
I'll not teach what is XMLor something like that, please read more documentation about XML if you haven't known that. We only focus on how to encode and decode XML files.
5
+
I will not make any attempts to teach XML's syntax or conventions. For that, please read more documentation about XML itself. We will only focus on how to encode and decode XML files in Go.
6
6
7
-
Suppose you are a operation staff, and you have following XML configuration file:
7
+
Suppose you work in IT, and you have to deal with the following XML configuration file:
8
8
9
9
<?xml version="1.0" encoding="utf-8"?>
10
10
<servers version="1">
@@ -18,15 +18,15 @@ Suppose you are a operation staff, and you have following XML configuration file
18
18
</server>
19
19
</servers>
20
20
21
-
Above XML document contains two kinds of information about your server, which are server name and IP; we will use this document in our following examples.
21
+
The above XML document contains two kinds of information about your server: the server name and IP. We will use this document in our following examples.
22
22
23
23
## Parse XML
24
24
25
-
How to parse this XML document? We can use function`Unmarshal` in package`xml` to do this.
25
+
How do we parse this XML document? We can use the`Unmarshal`function in Go's`xml` package to do this.
26
26
27
27
func Unmarshal(data []byte, v interface{}) error
28
28
29
-
datareceives data stream from XML, v is the structure you want to output, it is a interface, which means you can convert XML to any kind of structures. Here we only talk about how to convert to `struct`because they have similar tree structures.
29
+
the `data` parameter receives a data stream from an XML source, and `v` is the structure you want to output the parsed XML to. It is an interface, which means you can convert XML to any structure you desire. Here, we'll only talk about how to convert from XML to the `struct`type since they share similar tree structures.
30
30
31
31
Sample code:
32
32
@@ -74,7 +74,7 @@ Sample code:
74
74
fmt.Println(v)
75
75
}
76
76
77
-
XML actually is a tree data structure, and we can define a almost same struct in Go, then use `xml.Unmarshal` to convert from XML to our struct object. The sample code will print following content:
77
+
XML is actually a tree data structure, and we can define a very similar structure using structs in Go, then use `xml.Unmarshal` to convert from XML to our struct object. The sample code will print the following content:
@@ -87,42 +87,42 @@ XML actually is a tree data structure, and we can define a almost same struct in
87
87
</server>
88
88
}
89
89
90
-
We used`xml.Unmarshal` to parse XML document to corresponding struct object, and you should see that we have something like `xml:"serverName"` in our struct. This is a feature of struct which is called `struct tag` for helping reflection. Let's see the definition of `Unmarshal` again:
90
+
We use`xml.Unmarshal` to parse the XML document to the corresponding struct object. You should see that we have something like `xml:"serverName"` in our struct. This is a feature of structs called `struct tags` for helping with reflection. Let's see the definition of `Unmarshal` again:
91
91
92
92
func Unmarshal(data []byte, v interface{}) error
93
93
94
-
The first argument is XML data stream, the second argument is the type of storage, for now it supports struct, slice and string. XML package uses reflection to achieve data mapping, so all fields in v should be exported. But we still have a problem, how can it knows which field is corresponding to another one? Here is a priority level when parse data. It tries to find struct tag first, if it cannot find then get field name. Be aware that all tags, field name and XML element are case sensitive, so you have to make sure that one-one correspondence.
94
+
The first argument is an XML data stream. The second argument is storage type and supports the struct, slice and string types. Go's XML package uses reflection for data mapping, so all fields in v should be exported. However, this causes a problem: how can it know which XML field corresponds to the mapped struct field? The answer is that the XML parser parses data in a certain order. The library will try to find the matching struct tag first. If a match cannot be found then it searches through the struct field names. Be aware that all tags, field names and XML elements are case sensitive, so you have to make sure that there is a one to one correspondence for the mapping to succeed.
95
95
96
-
Go reflection mechanism allows you to use these tag information to reflect XML data to struct object. If you want to know more about reflection in Go, please read more about package documentation of struct tag and reflect.
96
+
Go's reflection mechanism allows you to use this tag information to reflect XML data to a struct object. If you want to know more about reflection in Go, please read the package documentation on struct tags and reflection.
97
97
98
-
Here are the rules when package `xml` parse XML document to struct:
98
+
Here are some rules when using the `xml`package to parse XML documents to structs:
99
99
100
-
- If the a field type is string or []byte with tag `",innerxml"`, `Unmarshal` assign raw XML data to it, like `Description` in above example:
100
+
- If the field type is a string or []byte with the tag `",innerxml"`, `Unmarshal`will assign raw XML data to it, like `Description` in the above example:
101
101
102
102
Shanghai_VPN127.0.0.1Beijing_VPN127.0.0.2
103
103
104
-
- If a field called `XMLName` and its type is `xml.Name`, then it gets element name, like `servers` in above example.
105
-
- If a field's tag contains corresponding element name, then it gets element name as well, like `servername` and `serverip` in above example.
106
-
- If a field's tag contains `",attr"`, then it gets corresponding element's attribute, like `version` in above example.
107
-
- If a field's tag contains something like `"a>b>c"`, it gets value of element c of node b of node a.
104
+
- If a field is called `XMLName` and its type is `xml.Name`, then it gets the element name, like `servers` in above example.
105
+
- If a field's tag contains the corresponding element name, then it gets the element name as well, like `servername` and `serverip` in the above example.
106
+
- If a field's tag contains `",attr"`, then it gets the corresponding element's attribute, like `version` in above example.
107
+
- If a field's tag contains something like `"a>b>c"`, it gets the value of the element c of node b of node a.
108
108
- If a field's tag contains `"="`, then it gets nothing.
109
-
- If a field's tag contains `",any"`, then it gets all child elements which do not fit other rules.
110
-
- If XML elements have one or more comments, all of these comments will be added to the first field that has the tag that contains `",comments"`, this field type can be string or []byte, if this kind field does not exist, all comments are discard.
109
+
- If a field's tag contains `",any"`, then it gets all child elements which do not fit the other rules.
110
+
- If the XML elements have one or more comments, all of these comments will be added to the first field that has the tag that contains `",comments"`. This field type can be a string or []byte. If this kind of field does not exist, all comments are discard.
111
111
112
-
These rules tell you how to define tags in struct, once you understand these rules, everything as easy as the sample code. Because tags and XML elements are one-one correspondence, we can also use slice to represent multiple elements in same level.
112
+
These rules tell you how to define tags in structs. Once you understand these rules, mapping XML to structs will be as easy as the sample code above. Because tags and XML elements have a one to one correspondence, we can also use slices to represent multiple elements on the same level.
113
113
114
-
Note that all fields in struct should be exported(capitalize) in order to parse data correctly.
114
+
Note that all fields in structs should be exported (capitalized) in order to parse data correctly.
115
115
116
116
## Produce XML
117
117
118
-
What if we want to produce XML document instead of parsing it, how can we do it in Go? `xml` package provides two functions which are `Marshal` and `MarshalIndent` where the second function has indents for your XML document. Their definition as follows:
118
+
What if we want to produce an XML document instead of parsing one. How do we do this in Go? Unsurprisingly, the `xml` package provides two functions which are `Marshal` and `MarshalIndent`, where the second function automatically indents the marshalled XML document. Their definition as follows:
The first argument is for storing XML data stream for both functions.
123
+
The first argument in both of these functions is for storing a marshalled XML data stream.
124
124
125
-
Let's has an example to see how it works:
125
+
Let's look at an example to see how this works:
126
126
127
127
package main
128
128
@@ -156,7 +156,7 @@ Let's has an example to see how it works:
156
156
os.Stdout.Write(output)
157
157
}
158
158
159
-
The above example prints following information:
159
+
The above example prints the following information:
160
160
161
161
<?xml version="1.0" encoding="UTF-8"?>
162
162
<servers version="1">
@@ -170,34 +170,34 @@ The above example prints following information:
170
170
</server>
171
171
</servers>
172
172
173
-
As we defined before, the reason we have `os.Stdout.Write([]byte(xml.Header))` is both of function `xml.MarshalIndent` and `xml.Marshal` do not output XML header by itself, so we have to print it in order to produce XML document correctly.
173
+
As we've previously defined, the reason we have `os.Stdout.Write([]byte(xml.Header))` is because both `xml.MarshalIndent` and `xml.Marshal` do not output XML headers on their own, so we have to explicitly print them in order to produce XML documents correctly.
174
174
175
-
Here we see `Marshal` also receives v in type `interface{}`, so what are the rules when it produces XML document?
175
+
Here we can see that `Marshal` also receives a v parameter of type `interface{}`. So what are the rules when marshalling to an XML document?
176
176
177
-
- If v is a array or slice, it prints all elements like value.
178
-
- If v is a pointer, it prints content that v point to, it prints nothing when v is nil.
179
-
- If v is a interface, it deal with interface as well.
180
-
- If v is one of other types, it prints value of that type.
177
+
- If v is an array or slice, it prints all elements like a value.
178
+
- If v is a pointer, it prints the content that v is pointing to, printing nothing when v is nil.
179
+
- If v is a interface, it deal with the interface as well.
180
+
- If v is one of the other types, it prints the value of that type.
181
181
182
-
So how can it decide elements' name? It follows following rules:
182
+
So how does `xml.Marshal` decide the elements' name? It follows the proceeding rules:
183
183
184
-
- If v is a struct, it defines name in tag of XMLName.
185
-
-Field name is XMLName and type is xml.Name.
184
+
- If v is a struct, it defines the name in the tag of XMLName.
185
+
-The field name is XMLName and the type is xml.Name.
186
186
- Field tag in struct.
187
187
- Field name in struct.
188
188
- Type name of marshal.
189
189
190
-
Then we need to figure out how to set tags in order to produce final XML document.
190
+
Then we need to figure out how to set tags in order to produce the final XML document.
191
191
192
192
- XMLName will not be printed.
193
-
- Fields that have tag contains`"-"` will not be printed.
194
-
- If tag contains `"name,attr"`, it uses name as attribute name and field value as value, like `version` in above example.
195
-
- If tag contains `",attr"`, it uses field's name as attribute name and field value as value.
196
-
- If tag contains `",chardata"`, it prints character data instead of element.
197
-
- If tag contains `",innerxml"`, it prints raw value.
198
-
- If tag contains `",comment"`, it prints it as comments without escaping, so you cannot have "--" in its value.
199
-
- If tag contains `"omitempty"`, it omits this field if its value is zero-value, including false, 0, nil pointer or nil interface, zero length of array, slice, map and string.
200
-
- If tag contains `"a>b>c"`, it prints three elements where a contains b, b contains c, like following code:
193
+
- Fields that have tags containing`"-"` will not be printed.
194
+
- If a tag contains `"name,attr"`, it uses name as the attribute name and the field value as the value, like `version` in the above example.
195
+
- If a tag contains `",attr"`, it uses the field's name as the attribute name and the field value as its value.
196
+
- If a tag contains `",chardata"`, it prints character data instead of element.
197
+
- If a tag contains `",innerxml"`, it prints the raw value.
198
+
- If a tag contains `",comment"`, it prints it as a comment without escaping, so you cannot have "--" in its value.
199
+
- If a tag contains `"omitempty"`, it omits this field if its value is zero-value, including false, 0, nil pointer or nil interface, zero length of array, slice, map and string.
200
+
- If a tag contains `"a>b>c"`, it prints three elements where a contains b and b contains c, like in the following code:
201
201
202
202
FirstName string `xml:"name>first"`
203
203
LastName string `xml:"name>last"`
@@ -207,10 +207,10 @@ Then we need to figure out how to set tags in order to produce final XML documen
207
207
<last>Xie</last>
208
208
</name>
209
209
210
-
You may notice that struct tag is very useful when you deal with XML, as well as other data format in following sections, if you still have problems with working with struct tag, you probably should read more documentation about it before get into next section.
210
+
You may have noticed that struct tags are very useful for dealing with XML, and the same goes for the other data formats we'll be discussing in the following sections. If you still find that you have problems with working with struct tags, you should probably read more documentation about them before diving into the next section.
0 commit comments