當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Unmarshal用法及代碼示例

GO語言"encoding/xml"包中"Unmarshal"函數的用法及代碼示例。

用法:

func Unmarshal(data []byte, v any) error

Unmarshal 解析 XML-encoded 數據並將結果存儲在 v 指向的值中,該值必須是任意結構、切片或字符串。不適合 v 的格式良好的數據將被丟棄。

因為 Unmarshal 使用反射包,它隻能分配給導出的(大寫)字段。 Unmarshal 使用區分大小寫的比較來將 XML 元素名稱與標記值和結構字段名稱匹配。

Unmarshal 使用以下規則將 XML 元素映射到結構。在規則中,字段的標簽是指與結構字段標簽中的鍵 'xml' 關聯的值(參見上麵的示例)。

* If the struct has a field of type []byte or string with tag
   ",innerxml", Unmarshal accumulates the raw XML nested inside the
   element in that field. The rest of the rules still apply.

* If the struct has a field named XMLName of type Name,
   Unmarshal records the element name in that field.

* If the XMLName field has an associated tag of the form
   "name" or "namespace-URL name", the XML element must have
   the given name (and, optionally, name space) or else Unmarshal
   returns an error.

* If the XML element has an attribute whose name matches a
   struct field name with an associated tag containing ",attr" or
   the explicit name in a struct field tag of the form "name,attr",
   Unmarshal records the attribute value in that field.

* If the XML element has an attribute not handled by the previous
   rule and the struct has a field with an associated tag containing
   ",any,attr", Unmarshal records the attribute value in the first
   such field.

* If the XML element contains character data, that data is
   accumulated in the first struct field that has tag ",chardata".
   The struct field may have type []byte or string.
   If there is no such field, the character data is discarded.

* If the XML element contains comments, they are accumulated in
   the first struct field that has tag ",comment".  The struct
   field may have type []byte or string. If there is no such
   field, the comments are discarded.

* If the XML element contains a sub-element whose name matches
   the prefix of a tag formatted as "a" or "a>b>c", unmarshal
   will descend into the XML structure looking for elements with the
   given names, and will map the innermost elements to that struct
   field. A tag starting with ">" is equivalent to one starting
   with the field name followed by ">".

* If the XML element contains a sub-element whose name matches
   a struct field's XMLName tag and the struct field has no
   explicit name tag as per the previous rule, unmarshal maps
   the sub-element to that struct field.

* If the XML element contains a sub-element whose name matches a
   field without any mode flags (",attr", ",chardata", etc), Unmarshal
   maps the sub-element to that struct field.

* If the XML element contains a sub-element that hasn't matched any
   of the above rules and the struct has a field with tag ",any",
   unmarshal maps the sub-element to that struct field.

* An anonymous struct field is handled as if the fields of its
   value were part of the outer struct.

* A struct field with tag "-" is never unmarshaled into.

如果 Unmarshal 遇到實現 Unmarshaler 接口的字段類型,Unmarshal 將調用其UnmarshalXML 方法從 XML 元素生成值。否則,如果該值實現編碼 TextUnmarshaler Unmarshal 調用該值的 UnmarshalText 方法。

Unmarshal 將 XML 元素映射到字符串或 [] 字節,方法是將元素的字符數據連接保存在字符串或 [] 字節中。保存的 []byte 永遠不會為零。

Unmarshal 通過將值保存在字符串或切片中來將屬性值映射到字符串或 []byte。

Unmarshal 通過將屬性(包括其名稱)保存在 Attr 中來將屬性值映射到 Attr。

Unmarshal 通過擴展切片的長度並將元素或屬性映射到新創建的值,將 XML 元素或屬性值映射到切片。

Unmarshal 通過將 XML 元素或屬性值設置為字符串表示的布爾值,將其映射到布爾值。空白被修剪和忽略。

Unmarshal 通過將字段設置為解釋十進製字符串值的結果,將 XML 元素或屬性值映射到整數或浮點字段。沒有檢查溢出。空白被修剪和忽略。

Unmarshal 通過記錄元素名稱將 XML 元素映射到 Name。

Unmarshal 將 XML 元素映射到指針,方法是將指針設置為新分配的值,然後將元素映射到該值。

缺少的元素或空屬性值將被解組為零值。如果該字段是一個切片,則該字段將附加一個零值。否則,該字段將設置為零值。

例子:

此示例演示將 XML 摘錄解組為具有一些預設字段的值。請注意,電話字段沒有被修改,並且 XML <Company> 元素被忽略。此外,考慮到其標簽中提供的元素路徑,分配 Groups 字段。

package main

import (
	"encoding/xml"
	"fmt"
)

func main() {
	type Email struct {
		Where string `xml:"where,attr"`
		Addr  string
	}
	type Address struct {
		City, State string
	}
	type Result struct {
		XMLName xml.Name `xml:"Person"`
		Name    string   `xml:"FullName"`
		Phone   string
		Email   []Email
		Groups  []string `xml:"Group>Value"`
		Address
	}
	v := Result{Name: "none", Phone: "none"}

	data := `
		<Person>
			<FullName>Grace R. Emlin</FullName>
			<Company>Example Inc.</Company>
			<Email where="home">
				<Addr>gre@example.com</Addr>
			</Email>
			<Email where='work'>
				<Addr>gre@work.com</Addr>
			</Email>
			<Group>
				<Value>Friends</Value>
				<Value>Squash</Value>
			</Group>
			<City>Hanga Roa</City>
			<State>Easter Island</State>
		</Person>
	`
	err := xml.Unmarshal([]byte(data), &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	fmt.Printf("XMLName: %#v\n", v.XMLName)
	fmt.Printf("Name: %q\n", v.Name)
	fmt.Printf("Phone: %q\n", v.Phone)
	fmt.Printf("Email: %v\n", v.Email)
	fmt.Printf("Groups: %v\n", v.Groups)
	fmt.Printf("Address: %v\n", v.Address)
}

輸出:

XMLName: xml.Name{Space:"", Local:"Person"}
Name: "Grace R. Emlin"
Phone: "none"
Email: [{home gre@example.com} {work gre@work.com}]
Groups: [Friends Squash]
Address: {Hanga Roa Easter Island}

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Unmarshal。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。