Standard library / encoding/xml

encoding/xml.Decoder.DecodeElement()

Decodes an element after its start token has already been read, useful for selective processing. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func (d *Decoder) DecodeElement(v any, start *StartElement) error

Parameters & Attributes

NameTypeDescription
vanyValue to encode or pointer to the destination for decoding.
start*StartElementStart element specifying the XML name and attributes.

Return Value

error

Nil on success; otherwise the operation error.

Working Example

Example Snippetgo
package main
import (  "encoding/xml"  "fmt"  "strings")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  d := xml.NewDecoder(strings.NewReader("<a>Go</a>"))  t, e := d.Token()  must(e)  start := t.(xml.StartElement)  var s string  must(d.DecodeElement(&s, &start))  fmt.Println(s)}
Go Reference