go - Golang marshal dynamic xml element name -



go - Golang marshal dynamic xml element name -

the xml file consists of 2 elements. elements have same construction except 1 element name. tried set value xmlname property, didn't work.

xml: <!-- first element --> <person> <elem1>...</elem1> <elem2>...</elem2> <elem3>...</elem3> <elem4>...</elem4> </person> <!-- sec element --> <sender> <elem1>...</elem1> <elem2>...</elem2> <elem3>...</elem3> <elem4>...</elem4> </sender>

is possible define struct such element name dynamic?

type person struct { xmlname string `xml:"???"` // how create dynamic? e1 string `xml:"elem1"` e2 string `xml:"elem2"` e3 string `xml:"elem3"` e4 string `xml:"elem4"` }

in documentation, says xmlname field must of type xml.name.

type person struct { xmlname xml.name e1 string `xml:"elem1"` // ... }

set element name via local field of xml.name:

person := person { xmlname: xml.name { local: "person" }, // ... }

(also, e1 - e4 must exported in order included in xml output).

playground example: http://play.golang.org/p/bzsutff9bo

xml go marshalling unmarshalling

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -