Golang encoding/xml.Encoder.Indent() function example
package encoding/xml
Indent sets the encoder to generate XML in which each element begins on a new indented line that starts with prefix and is followed by one or more copies of indent according to the nesting depth.
Golang encoding/xml.Encoder.Indent() function usage example
package main
import (
"encoding/xml"
"fmt"
"os"
"reflect"
)
type Address struct {
City, State string
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
encoder.Indent(" ", " ") // <---- here
newtoken := xml.StartElement{
Name : xml.Name{
Space: "",
Local: "location",
},
}
if err := encoder.EncodeToken(newtoken); err != nil {
fmt.Println(err)
}
start := xml.StartElement{
Name: xml.Name{
Space: "",
Local: reflect.Indirect(reflect.ValueOf(v)).Type().Name(),
},
Attr: []xml.Attr{{xml.Name{"", "xmlns"}, "https://socketloop.com/xmldoc/2014-09-01/"}},
}
if err := encoder.EncodeElement(v, start); err != nil {
fmt.Println(err)
}
encoder.Flush()
}
Reference :
Advertisement
Something interesting
Tutorials
+22.7k Golang : Strings to lowercase and uppercase example
+13.8k Golang : unknown escape sequence error
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+9.3k Golang : How to get ECDSA curve and parameters data?
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+20.2k Golang : How to get own program name during runtime ?
+14.2k Golang : Chunk split or divide a string into smaller chunk example
+7.1k Golang : Transform lisp or spinal case to Pascal case example
+14.6k Golang : Execute function at intervals or after some delay