Update 07.1.md
typographical errors and improved readability
This commit is contained in:
@@ -91,7 +91,7 @@ We use `xml.Unmarshal` to parse the XML document to the corresponding struct obj
|
||||
|
||||
func Unmarshal(data []byte, v interface{}) error
|
||||
|
||||
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.
|
||||
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 does 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'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.
|
||||
|
||||
@@ -107,9 +107,9 @@ Here are some rules when using the `xml` package to parse XML documents to struc
|
||||
- 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 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.
|
||||
- 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 discarded.
|
||||
|
||||
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.
|
||||
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 structs should be exported (capitalized) in order to parse data correctly.
|
||||
|
||||
@@ -179,7 +179,7 @@ Here we can see that `Marshal` also receives a v parameter of type `interface{}`
|
||||
- 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 does `xml.Marshal` decide the elements' name? It follows the proceeding rules:
|
||||
So how does `xml.Marshal` decide the elements' name? It follows the ensuing rules:
|
||||
|
||||
- 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.
|
||||
|
||||
Reference in New Issue
Block a user