当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。