U
U
UntitledNikname2020-11-04 12:13:09
go
UntitledNikname, 2020-11-04 12:13:09

How to create xml with golang attributes?

Like from

type MyData struct {
  Name string
  Attributes map[string]string
}

do
<name atr1="v1" atr2="v2"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2020-11-04
@UntitledNikname

Something like this: https://play.golang.com/p/TqlsfIV2BSF

package main

import (
  "encoding/xml"
  "fmt"
)

type MyData struct {
  XMLName    xml.Name
  Attributes []xml.Attr `xml:",attr"`
}

func main() {
  v := MyData{
    XMLName: xml.Name{Local: "somename"},
    Attributes: []xml.Attr{
      {Name: xml.Name{Local: "attr1"}, Value: "val1"},
      {Name: xml.Name{Local: "attr2"}, Value: "val2"},
    },
  }

  rawData, _ := xml.Marshal(v)
  fmt.Println(string(rawData))
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question