如何用冒号解组 xml 元素
How to unmarshal xml element with colon
由于代码错误,我无法在 Go 中用冒号解组 xml 元素。
https://play.golang.org/p/zMm5IQSfS01
package main
import (
"fmt"
"encoding/xml"
)
type Obj struct {
XMLName xml.Name `xml:"book"`
AbcName string `xml:"abc:name"`
}
func main() {
a := []byte(`<book><abc:name>text</abc:name></book>`)
obj := &Obj{}
if err := xml.Unmarshal(a, obj); err != nil {
panic(err)
}
fmt.Printf("%#v\n", obj)
}
代码输出:
&main.Obj{XMLName:xml.Name{Space:"", Local:"book"}, AbcName:""}
是否可以在 Golang 中用冒号解组 xml 元素?如果是,如何?
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.
type Obj struct {
XMLName xml.Name `xml:"book"`
AbcName string `xml:"abc name"`
DefName string `xml:"def name"`
}
func main() {
a := []byte(`<book><abc:name>text</abc:name><def:name>some other text</def:name></book>`)
obj := &Obj{}
if err := xml.Unmarshal(a, obj); err != nil {
panic(err)
}
fmt.Printf("%#v\n", obj)
}
由于代码错误,我无法在 Go 中用冒号解组 xml 元素。
https://play.golang.org/p/zMm5IQSfS01
package main
import (
"fmt"
"encoding/xml"
)
type Obj struct {
XMLName xml.Name `xml:"book"`
AbcName string `xml:"abc:name"`
}
func main() {
a := []byte(`<book><abc:name>text</abc:name></book>`)
obj := &Obj{}
if err := xml.Unmarshal(a, obj); err != nil {
panic(err)
}
fmt.Printf("%#v\n", obj)
}
代码输出:
&main.Obj{XMLName:xml.Name{Space:"", Local:"book"}, AbcName:""}
是否可以在 Golang 中用冒号解组 xml 元素?如果是,如何?
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.
type Obj struct {
XMLName xml.Name `xml:"book"`
AbcName string `xml:"abc name"`
DefName string `xml:"def name"`
}
func main() {
a := []byte(`<book><abc:name>text</abc:name><def:name>some other text</def:name></book>`)
obj := &Obj{}
if err := xml.Unmarshal(a, obj); err != nil {
panic(err)
}
fmt.Printf("%#v\n", obj)
}