Golang encoding/xml: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 42: | Line 42: | ||
* It doesn't appear that go's internal xml library supports schema validation | * It doesn't appear that go's internal xml library supports schema validation | ||
</blockquote><!-- Struct Tags --> | </blockquote><!-- Struct Tags --> | ||
= Encoding = | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
package main | |||
import ( | |||
"encoding/xml" | |||
"fmt" | |||
) | |||
type User struct { | |||
Id int `xml:"id"` | |||
Name string `xml:"name,attr"` | |||
} | |||
func main() { | |||
user := User{123, "will"} | |||
bytes, _ := xml.Marshal(&user) | |||
fmt.Println(string(bytes)) // <User name="will"><id>123</id></User> | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Encoding --> | |||
= Decoding = | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
type User struct { | |||
Id int `xml:"id"` | |||
Name string `xml:"name,attr"` | |||
} | |||
func main() { | |||
user := User{123, "will"} | |||
bytes, _ := xml.Marshal(&user) | |||
fmt.Println(string(bytes)) // <User name="will"><id>123</id></User> | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Decoding --> | |||
= Encoding = | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
type User struct { | |||
Id int `xml:"id"` | |||
Name string `xml:"name,attr"` | |||
} | |||
func main() { | |||
var user User | |||
encoded := []byte(`<User name="foo"><id>123</id></User>`) | |||
xml.Unmarshal(encoded, &user) | |||
fmt.Println(user) | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Encoding --> |
Revision as of 00:21, 26 June 2022
go's builtin library for parsing xml.
See also: golang encoding, xml
A more detailed introduction to go's encoding interface can be seen in golang encoding/json.
NOTE:
Golang's builtin xml library does not support the full xml spec.
Confirm your needs are supported before using it
Documentation
official docs https://pkg.go.dev/encoding/xml@go1.18.3
Tutorials
tutorialedge https://tutorialedge.net/golang/parsing-xml-with-golang/
Struct Tags
See full details here
type User struct { Name `xml:"Name"` // <User><Name>value</Name></User> Color `xml:"color,attr"` // <User color="value"></User> Skip `xml:"-"` // <User></User> }Known issues:
- xml namespaces don't work as expected: https://github.com/golang/go/issues/11496
- It doesn't appear that go's internal xml library supports schema validation
Encoding
package main import ( "encoding/xml" "fmt" ) type User struct { Id int `xml:"id"` Name string `xml:"name,attr"` } func main() { user := User{123, "will"} bytes, _ := xml.Marshal(&user) fmt.Println(string(bytes)) // <User name="will"><id>123</id></User> }
Decoding
type User struct { Id int `xml:"id"` Name string `xml:"name,attr"` } func main() { user := User{123, "will"} bytes, _ := xml.Marshal(&user) fmt.Println(string(bytes)) // <User name="will"><id>123</id></User> }
Encoding
type User struct { Id int `xml:"id"` Name string `xml:"name,attr"` } func main() { var user User encoded := []byte(`<User name="foo"><id>123</id></User>`) xml.Unmarshal(encoded, &user) fmt.Println(user) }