Im working on my own MarshalXML function to get required output format for an input object, but cant seem to find a proper way of giving tags a proper names.
Let say i have:
type RootElement struct{
XMLName xml.Name `xml:"hello"`
world ChildElement `xml:"world"`
}
And a child element:
type ChildElement struct{
Value string
}
Then Encode method in MarshalXML for RootElement would give me:
<RootElement>
<ChildElement>
...
</ChildElement>
</RootElement>
Instead of that id have to create my own callset of EncodeToken methods in order to put there proper tag names, but even here i have to specify xml.Name as a constant string value even though i already have it defined in the model structs.
Is there some way to get 'hello' and 'world' tag names inside XMLMarshal?
I'm not sure why the RootElement name didn't work, but for the ChildElement, you have to put the tag xml:",chardata"
, so the Value is detected as the ChildElement's value and not a node.
http://play.golang.org/p/pg2vhD8wMf
package main
import (
"encoding/xml"
"fmt"
)
type RootElement struct{
XMLName xml.Name `xml:"hello"`
World ChildElement `xml:"world",`
}
type ChildElement struct{
Value string `xml:",chardata"`
}
func main() {
r := RootElement{World: ChildElement{Value: "child value"}}
m, err := xml.MarshalIndent(r, "", " ")
if err == nil {
fmt.Printf("%s
", m)
}
}