Fix grammar and improve sentence flow for 07.1md, fix typo in 06.3.md
This commit is contained in:
@@ -115,7 +115,7 @@ We introduced a simple session manager's working principles in the previous sect
|
||||
}
|
||||
|
||||
|
||||
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?
|
||||
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?
|
||||
|
||||
import (
|
||||
"github.com/astaxie/session"
|
||||
@@ -136,4 +136,4 @@ We use the blank import mechanism (which will invoke the package's `init()` func
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [How to use sessions in Go](06.2.md)
|
||||
- Next section: [Prevent hijack of session](06.4.md)
|
||||
- Next section: [Prevent session hijacking](06.4.md)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# 7.1 XML
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
I'll not teach what is XML or 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.
|
||||
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.
|
||||
|
||||
Suppose you are a operation staff, and you have following XML configuration file:
|
||||
Suppose you work in IT, and you have to deal with the following XML configuration file:
|
||||
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<servers version="1">
|
||||
@@ -18,15 +18,15 @@ Suppose you are a operation staff, and you have following XML configuration file
|
||||
</server>
|
||||
</servers>
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
## Parse XML
|
||||
|
||||
How to parse this XML document? We can use function `Unmarshal` in package `xml` to do this.
|
||||
How do we parse this XML document? We can use the `Unmarshal` function in Go's `xml` package to do this.
|
||||
|
||||
func Unmarshal(data []byte, v interface{}) error
|
||||
|
||||
data receives 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.
|
||||
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.
|
||||
|
||||
Sample code:
|
||||
|
||||
@@ -74,7 +74,7 @@ Sample code:
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
{{ servers} 1 [{{ server} Shanghai_VPN 127.0.0.1} {{ server} Beijing_VPN 127.0.0.2}]
|
||||
<server>
|
||||
@@ -87,42 +87,42 @@ XML actually is a tree data structure, and we can define a almost same struct in
|
||||
</server>
|
||||
}
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
func Unmarshal(data []byte, v interface{}) error
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
Here are the rules when package `xml` parse XML document to struct:
|
||||
Here are some rules when using the `xml` package to parse XML documents to structs:
|
||||
|
||||
- If the a field type is string or []byte with tag `",innerxml"`, `Unmarshal` assign raw XML data to it, like `Description` in above example:
|
||||
- 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:
|
||||
|
||||
Shanghai_VPN127.0.0.1Beijing_VPN127.0.0.2
|
||||
|
||||
- If a field called `XMLName` and its type is `xml.Name`, then it gets element name, like `servers` in above example.
|
||||
- If a field's tag contains corresponding element name, then it gets element name as well, like `servername` and `serverip` in above example.
|
||||
- If a field's tag contains `",attr"`, then it gets corresponding element's attribute, like `version` in above example.
|
||||
- If a field's tag contains something like `"a>b>c"`, it gets value of element c of node b of node a.
|
||||
- If a field is called `XMLName` and its type is `xml.Name`, then it gets the element name, like `servers` in above example.
|
||||
- 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.
|
||||
- If a field's tag contains `",attr"`, then it gets the corresponding element's attribute, like `version` in above example.
|
||||
- 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.
|
||||
- If a field's tag contains `"="`, then it gets nothing.
|
||||
- If a field's tag contains `",any"`, then it gets all child elements which do not fit other rules.
|
||||
- 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.
|
||||
- If a field's tag contains `",any"`, then it gets all child elements which do not fit the other rules.
|
||||
- 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.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
Note that all fields in struct should be exported(capitalize) in order to parse data correctly.
|
||||
Note that all fields in structs should be exported (capitalized) in order to parse data correctly.
|
||||
|
||||
## Produce XML
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
func Marshal(v interface{}) ([]byte, error)
|
||||
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
|
||||
|
||||
The first argument is for storing XML data stream for both functions.
|
||||
The first argument in both of these functions is for storing a marshalled XML data stream.
|
||||
|
||||
Let's has an example to see how it works:
|
||||
Let's look at an example to see how this works:
|
||||
|
||||
package main
|
||||
|
||||
@@ -156,7 +156,7 @@ Let's has an example to see how it works:
|
||||
os.Stdout.Write(output)
|
||||
}
|
||||
|
||||
The above example prints following information:
|
||||
The above example prints the following information:
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<servers version="1">
|
||||
@@ -170,34 +170,34 @@ The above example prints following information:
|
||||
</server>
|
||||
</servers>
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
Here we see `Marshal` also receives v in type `interface{}`, so what are the rules when it produces XML document?
|
||||
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?
|
||||
|
||||
- If v is a array or slice, it prints all elements like value.
|
||||
- If v is a pointer, it prints content that v point to, it prints nothing when v is nil.
|
||||
- If v is a interface, it deal with interface as well.
|
||||
- If v is one of other types, it prints value of that type.
|
||||
- If v is an array or slice, it prints all elements like a value.
|
||||
- If v is a pointer, it prints the content that v is pointing to, printing nothing when v is nil.
|
||||
- If v is a interface, it deal with the interface as well.
|
||||
- If v is one of the other types, it prints the value of that type.
|
||||
|
||||
So how can it decide elements' name? It follows following rules:
|
||||
So how does `xml.Marshal` decide the elements' name? It follows the proceeding rules:
|
||||
|
||||
- If v is a struct, it defines name in tag of XMLName.
|
||||
- Field name is XMLName and type is xml.Name.
|
||||
- If v is a struct, it defines the name in the tag of XMLName.
|
||||
- The field name is XMLName and the type is xml.Name.
|
||||
- Field tag in struct.
|
||||
- Field name in struct.
|
||||
- Type name of marshal.
|
||||
|
||||
Then we need to figure out how to set tags in order to produce final XML document.
|
||||
Then we need to figure out how to set tags in order to produce the final XML document.
|
||||
|
||||
- XMLName will not be printed.
|
||||
- Fields that have tag contains `"-"` will not be printed.
|
||||
- If tag contains `"name,attr"`, it uses name as attribute name and field value as value, like `version` in above example.
|
||||
- If tag contains `",attr"`, it uses field's name as attribute name and field value as value.
|
||||
- If tag contains `",chardata"`, it prints character data instead of element.
|
||||
- If tag contains `",innerxml"`, it prints raw value.
|
||||
- If tag contains `",comment"`, it prints it as comments without escaping, so you cannot have "--" in its value.
|
||||
- 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.
|
||||
- If tag contains `"a>b>c"`, it prints three elements where a contains b, b contains c, like following code:
|
||||
- Fields that have tags containing `"-"` will not be printed.
|
||||
- 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.
|
||||
- If a tag contains `",attr"`, it uses the field's name as the attribute name and the field value as its value.
|
||||
- If a tag contains `",chardata"`, it prints character data instead of element.
|
||||
- If a tag contains `",innerxml"`, it prints the raw value.
|
||||
- If a tag contains `",comment"`, it prints it as a comment without escaping, so you cannot have "--" in its value.
|
||||
- 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.
|
||||
- If a tag contains `"a>b>c"`, it prints three elements where a contains b and b contains c, like in the following code:
|
||||
|
||||
FirstName string `xml:"name>first"`
|
||||
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
|
||||
<last>Xie</last>
|
||||
</name>
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Text files](07.0.md)
|
||||
- Next section: [JSON](07.2.md)
|
||||
- Next section: [JSON](07.2.md)
|
||||
|
||||
Reference in New Issue
Block a user