-
Notifications
You must be signed in to change notification settings - Fork 4
/
anyxml.go
222 lines (204 loc) · 4.96 KB
/
anyxml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// anyxml - marshal an XML document from almost any Go variable.
// Copyright 2012-2019, Charles Banning. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file
// Marshal XML from map[string]interface{}, arrays, slices, alpha/numeric, etc.
//
// Wraps xml.Marshal with functionality in github.com/clbanning/mxj to create
// a more genericized XML marshaling capability. Note: unmarshaling the resultant
// XML may not return the original value, since tag labels may have been injected
// to create the XML representation of the value.
//
// See mxj package documentation for more information. See anyxml_test.go for
// examples or just try Xml() or XmlIndent().
/*
Encode an arbitrary JSON object.
package main
import (
"encoding/json"
"fmt"
"github.com/clbanning/anyxml"
)
func main() {
jsondata := []byte(`[
{ "somekey":"somevalue" },
"string",
3.14159265,
true
]`)
var i interface{}
err := json.Unmarshal(jsondata, &i)
if err != nil {
// do something
}
x, err := anyxml.XmlIndent(i, "", " ", "mydoc")
if err != nil {
// do something else
}
fmt.Println(string(x))
}
output:
<mydoc>
<somekey>somevalue</somekey>
<element>string</element>
<element>3.14159265</element>
<element>true</element>
</mydoc>
An example of encoding a map[interface{}]interface{} value with mixed key types is
in anyxml/examples/goofy_map.go.
*/
package anyxml
import (
"bytes"
"encoding/xml"
"io"
"reflect"
)
// Default missingElementTag value.
var missingElemTag = "element"
// MissingElementTag is used to set the lable to be used
// for values that are not map[string]interface{} type. By default
// the tag label "element" is used. The default can be reset by
// passing an empty string, "", argument: MissingElementTag("").
func MissingElementTag(s string) {
if s == "" {
missingElemTag = "element"
}
missingElemTag = s
}
// Encode arbitrary value as XML. Note: there are no guarantees.
func Xml(v interface{}, rootTag ...string) ([]byte, error) {
var rt string
if len(rootTag) == 1 {
rt = rootTag[0]
} else {
rt = DefaultRootTag
}
if v == nil {
if UseGoEmptyElementSyntax {
return []byte("<" + rt + "></" + rt + ">"), nil
}
return []byte("<" + rt + "/>"), nil
}
if reflect.TypeOf(v).Kind() == reflect.Struct {
return xml.Marshal(v)
}
var err error
s := new(string)
p := new(pretty)
var ss string
var b []byte
switch v.(type) {
case []interface{}:
ss = "<" + rt + ">"
for _, vv := range v.([]interface{}) {
switch vv.(type) {
case map[string]interface{}:
m := vv.(map[string]interface{})
if len(m) == 1 {
for tag, val := range m {
err = mapToXmlIndent(false, s, tag, val, p)
}
} else {
err = mapToXmlIndent(false, s, missingElemTag, vv, p)
}
default:
err = mapToXmlIndent(false, s, missingElemTag, vv, p)
}
if err != nil {
break
}
}
ss += *s + "</" + rt + ">"
b = []byte(ss)
case map[string]interface{}:
b, err = anyxml(v.(map[string]interface{}), rootTag...)
default:
err = mapToXmlIndent(false, s, rt, v, p)
b = []byte(*s)
}
if xmlCheckIsValid {
d := xml.NewDecoder(bytes.NewReader(b))
for {
_, err = d.Token()
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
}
}
return b, err
}
// Encode an arbitrary value as a pretty XML string. Note: there are no guarantees.
func XmlIndent(v interface{}, prefix, indent string, rootTag ...string) ([]byte, error) {
var rt string
if len(rootTag) == 1 {
rt = rootTag[0]
} else {
rt = DefaultRootTag
}
if v == nil {
if UseGoEmptyElementSyntax {
return []byte(prefix + "<" + rt + "></" + rt + ">"), nil
}
return []byte(prefix + "<" + rt + "/>"), nil
}
if reflect.TypeOf(v).Kind() == reflect.Struct {
return xml.MarshalIndent(v, prefix, indent)
}
var err error
s := new(string)
p := new(pretty)
p.indent = indent
p.padding = prefix
var ss string
var b []byte
switch v.(type) {
case []interface{}:
ss = "<" + rt + ">\n"
p.Indent()
for _, vv := range v.([]interface{}) {
switch vv.(type) {
case map[string]interface{}:
m := vv.(map[string]interface{})
if len(m) == 1 {
for tag, val := range m {
err = mapToXmlIndent(true, s, tag, val, p)
}
} else {
p.start = 1 // we're 1 tag in to the doc
err = mapToXmlIndent(true, s, missingElemTag, vv, p)
*s += "\n"
}
default:
p.start = 0
err = mapToXmlIndent(true, s, missingElemTag, vv, p)
}
if err != nil {
break
}
}
ss += *s + "</" + rt + ">"
b = []byte(ss)
case map[string]interface{}:
b, err = anyxmlIndent(v.(map[string]interface{}), prefix, indent, rootTag...)
default:
err = mapToXmlIndent(true, s, rt, v, p)
b = []byte(*s)
}
if xmlCheckIsValid {
d := xml.NewDecoder(bytes.NewReader(b))
for {
_, err = d.Token()
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
}
}
return b, err
}