1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
17
// A generic XML header suitable for use with the output of Marshal and MarshalIndent.
18
// This is not automatically added to any output of this package, it is provided as a
20
Header = `<?xml version="1.0" encoding="UTF-8">\n`
23
// A Marshaler can produce well-formatted XML representing its internal state.
24
// It is used by both Marshal and MarshalIndent.
25
type Marshaler interface {
26
MarshalXML() ([]byte, os.Error)
33
// Marshal writes an XML-formatted representation of v to w.
35
// If v implements Marshaler, then Marshal calls its MarshalXML method.
36
// Otherwise, Marshal uses the following procedure to create the XML.
38
// Marshal handles an array or slice by marshalling each of the elements.
39
// Marshal handles a pointer by marshalling the value it points at or, if the
40
// pointer is nil, by writing nothing. Marshal handles an interface value by
41
// marshalling the value it contains or, if the interface value is nil, by
42
// writing nothing. Marshal handles all other data by writing a single XML
43
// element containing the data.
45
// The name of that XML element is taken from, in order of preference:
46
// - the tag on an XMLName field, if the data is a struct
47
// - the value of an XMLName field of type xml.Name
48
// - the tag of the struct field used to obtain the data
49
// - the name of the struct field used to obtain the data
52
// The XML element for a struct contains marshalled elements for each of the
53
// exported fields of the struct, with these exceptions:
54
// - the XMLName field, described above, is omitted.
55
// - a field with tag "attr" becomes an attribute in the XML element.
56
// - a field with tag "chardata" is written as character data,
57
// not as an XML element.
58
// - a field with tag "innerxml" is written verbatim,
59
// not subject to the usual marshalling procedure.
61
// Marshal will return an error if asked to marshal a channel, function, or map.
62
func Marshal(w io.Writer, v interface{}) (err os.Error) {
63
p := &printer{bufio.NewWriter(w)}
64
err = p.marshalValue(reflect.ValueOf(v), "???")
69
func (p *printer) marshalValue(val reflect.Value, name string) os.Error {
78
if typ.NumMethod() > 0 {
79
if marshaler, ok := val.Interface().(Marshaler); ok {
80
bytes, err := marshaler.MarshalXML()
89
// Drill into pointers/interfaces
90
if kind == reflect.Ptr || kind == reflect.Interface {
94
return p.marshalValue(val.Elem(), name)
97
// Slices and arrays iterate over the elements. They do not have an enclosing tag.
98
if (kind == reflect.Slice || kind == reflect.Array) && typ.Elem().Kind() != reflect.Uint8 {
99
for i, n := 0, val.Len(); i < n; i++ {
100
if err := p.marshalValue(val.Index(i), name); err != nil {
109
if kind == reflect.Struct {
110
if f, ok := typ.FieldByName("XMLName"); ok {
111
if tag := f.Tag.Get("xml"); tag != "" {
112
if i := strings.Index(tag, " "); i >= 0 {
113
xmlns, name = tag[:i], tag[i+1:]
117
} else if v, ok := val.FieldByIndex(f.Index).Interface().(Name); ok && v.Local != "" {
118
xmlns, name = v.Space, v.Local
127
if kind == reflect.Struct {
129
p.WriteString(` xmlns="`)
130
Escape(p, []byte(xmlns))
134
for i, n := 0, typ.NumField(); i < n; i++ {
135
if f := typ.Field(i); f.PkgPath == "" && f.Tag.Get("xml") == "attr" {
136
if f.Type.Kind() == reflect.String {
137
if str := val.Field(i).String(); str != "" {
139
p.WriteString(strings.ToLower(f.Name))
141
Escape(p, []byte(str))
150
switch k := val.Kind(); k {
151
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
152
p.WriteString(strconv.Itoa64(val.Int()))
153
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
154
p.WriteString(strconv.Uitoa64(val.Uint()))
155
case reflect.Float32, reflect.Float64:
156
p.WriteString(strconv.Ftoa64(val.Float(), 'g', -1))
158
Escape(p, []byte(val.String()))
160
p.WriteString(strconv.Btoa(val.Bool()))
163
bytes := make([]byte, val.Len())
164
for i := range bytes {
165
bytes[i] = val.Index(i).Interface().(byte)
170
bytes := val.Interface().([]byte)
173
for i, n := 0, val.NumField(); i < n; i++ {
174
if f := typ.Field(i); f.Name != "XMLName" && f.PkgPath == "" {
176
switch tag := f.Tag.Get("xml"); tag {
179
if tk := f.Type.Kind(); tk == reflect.String {
180
p.Write([]byte(val.Field(i).String()))
181
} else if tk == reflect.Slice {
182
if elem, ok := val.Field(i).Interface().([]byte); ok {
188
iface := val.Field(i).Interface()
189
switch raw := iface.(type) {
203
if err := p.marshalValue(val.Field(i), name); err != nil {
209
return &UnsupportedTypeError{typ}
220
// A MarshalXMLError is returned when Marshal or MarshalIndent encounter a type
221
// that cannot be converted into XML.
222
type UnsupportedTypeError struct {
226
func (e *UnsupportedTypeError) String() string {
227
return "xml: unsupported type: " + e.Type.String()