go-xml icon indicating copy to clipboard operation
go-xml copied to clipboard

add XMLName field to generated structs

Open SoMuchForSubtlety opened this issue 4 years ago • 3 comments

Without this PR, marshalling a struct generated by xsdgen will not set it's name and namespace correctly. As fas as I can tell the standard practice in go is to add the field XMLName of type xml.Name to structs you want to marshal to XML. That fiel's tag will then be used for setting the elements name and namespace.

see https://pkg.go.dev/encoding/xml#Marshal

SoMuchForSubtlety avatar Aug 08 '21 14:08 SoMuchForSubtlety

I just realized, that this should only be done for top level elements. I'll need to figure out how to determine that at the code generation stage.

SoMuchForSubtlety avatar Aug 08 '21 16:08 SoMuchForSubtlety

I think you need to manually create a top level struct with the XMLName xml.Name field and embed the top level structure you want to unmarshall using that

jimidle avatar Mar 22 '22 03:03 jimidle