xml.go 691 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "bytes"
  7. "encoding/xml"
  8. "io"
  9. "net/http"
  10. )
  11. type xmlBinding struct{}
  12. func (xmlBinding) Name() string {
  13. return "xml"
  14. }
  15. func (xmlBinding) Bind(req *http.Request, obj interface{}) error {
  16. return decodeXML(req.Body, obj)
  17. }
  18. func (xmlBinding) BindBody(body []byte, obj interface{}) error {
  19. return decodeXML(bytes.NewReader(body), obj)
  20. }
  21. func decodeXML(r io.Reader, obj interface{}) error {
  22. decoder := xml.NewDecoder(r)
  23. if err := decoder.Decode(obj); err != nil {
  24. return err
  25. }
  26. return nil
  27. }