This wraps encoding/xml with github.com/clbanning/mxj functionality. See mxj package documentation for caveats, etc.
- 'nil' Map values, which may represent 'null' JSON values, are encoded as '<tag/>' unless XmlGoEmptyElemSyntax() has been called to change the default to encoding/xml syntax, '<tag></tag>'.
- map[string]interface{} keys that are prepended by a hyphen, '-', are assumed to be attribute labels.
Since some values, such as arrays, may require injecting tag labels to generate the XML, unmarshaling the resultant XML is not necessarily symmetric, i.e., you cannot get the original value back without some manipulation.
http://godoc.org/github.com/clbanning/anyxml
Encode an arbitrary JSON object.
package main
import (
"encoding/json"
"fmt"
"github.com/clbanning/anyxml"
)
func main() {
jasondata := []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 with mixed value types is in anyxml/examples/goofy_map.go.