From 3c2da979192acd7570d201999556e21076ed8b98 Mon Sep 17 00:00:00 2001 From: Chad Russell Date: Fri, 4 Jul 2014 20:51:25 -0400 Subject: [PATCH 1/2] Fix XML Marshal to work with gin.H --- gin.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gin.go b/gin.go index 197b98c..bf2338e 100644 --- a/gin.go +++ b/gin.go @@ -62,6 +62,17 @@ type ( } ) +// Allows type H to be used with xml.Marshal +func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + e.EncodeToken(start) + for key, value := range h { + elem := xml.StartElement{Name: xml.Name{Local: key}} + e.EncodeElement(value, elem) + } + e.EncodeToken(xml.EndElement{start.Name}) + return nil +} + func (a ErrorMsgs) String() string { var buffer bytes.Buffer for i, msg := range a { From c978efa42f3e072a1969c42bfa5305131d0a7935 Mon Sep 17 00:00:00 2001 From: Chad Russell Date: Fri, 4 Jul 2014 23:18:05 -0400 Subject: [PATCH 2/2] added error handling --- gin.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/gin.go b/gin.go index bf2338e..308a34c 100644 --- a/gin.go +++ b/gin.go @@ -64,12 +64,21 @@ type ( // Allows type H to be used with xml.Marshal func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - e.EncodeToken(start) - for key, value := range h { - elem := xml.StartElement{Name: xml.Name{Local: key}} - e.EncodeElement(value, elem) + if err := e.EncodeToken(start); err != nil { + return err + } + for key, value := range h { + elem := xml.StartElement{ + xml.Name{"", key}, + []xml.Attr{}, + } + if err = e.EncodeElement(value, elem); err != nil { + return err + } + } + if err = e.EncodeToken(xml.EndElement{start.Name}); err != nil { + return err } - e.EncodeToken(xml.EndElement{start.Name}) return nil }