Golang encoding/xml: Difference between revisions
From wikinotes
Line 67: | Line 67: | ||
= Decoding = | = Decoding = | ||
<blockquote> | |||
== Basics == | |||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
Line 80: | Line 82: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Basics --> | |||
== Non-Homogenous XML == | |||
<blockquote> | |||
XML is generally not homogenous.<br> | |||
Record each possible sub-element as a field on your object.<br> | |||
If an element can occur multiple times, declare it as an array.<br> | |||
You can ignore elements by not defining fields for them. | |||
<syntaxhighlight lang="go"> | |||
encoded := ` | |||
<mediawiki> | |||
<siteinfo> | |||
abc | |||
</siteinfo> | |||
<page> | |||
<title>Main Page</title> | |||
</page> | |||
<page> | |||
<title>Linux</title> | |||
</page> | |||
</mediawiki> | |||
` | |||
type Result struct { | |||
XMLName xml.Name `xml:"mediawiki"` // root node | |||
SiteInfo string `xml:"siteinfo"` // only one 'siteinfo' element under 'mediawiki' | |||
Page []Page `xml:"page"` // multiple 'page' elements under 'mediawiki' | |||
} | |||
type Page struct { | |||
Title string `xml:"title"` | |||
} | |||
var result Result | |||
xml.Unmarshall([]byte(encoded), &result) | |||
fmt.Println(result.Page[0].Title.Text) // Linux | |||
</syntaxhighlight> | |||
</blockquote><!-- Non-Homogenous XML --> | |||
</blockquote><!-- Decoding --> | </blockquote><!-- Decoding --> | ||
Revision as of 02:48, 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 are not supported https://github.com/golang/go/issues/11496
- schema validation does not appear to be supported
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
Basics
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> }Non-Homogenous XML
XML is generally not homogenous.
Record each possible sub-element as a field on your object.
If an element can occur multiple times, declare it as an array.
You can ignore elements by not defining fields for them.encoded := ` <mediawiki> <siteinfo> abc </siteinfo> <page> <title>Main Page</title> </page> <page> <title>Linux</title> </page> </mediawiki> ` type Result struct { XMLName xml.Name `xml:"mediawiki"` // root node SiteInfo string `xml:"siteinfo"` // only one 'siteinfo' element under 'mediawiki' Page []Page `xml:"page"` // multiple 'page' elements under 'mediawiki' } type Page struct { Title string `xml:"title"` } var result Result xml.Unmarshall([]byte(encoded), &result) fmt.Println(result.Page[0].Title.Text) // Linux
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) }