github.com/chrismalek/anyxml

wraps xml.Marshal() with github.com/clbanning/mxj general XML marshaling capability


License
Other
Install
go get github.com/chrismalek/anyxml

Documentation

anyxml - create an XML document from almost any Go type

Marshal XML from map[string]interface{}, arrays, slices, and alpha/numeric values.

This wraps encoding/xml with github.com/clbanning/mxj functionality. See mxj package documentation for caveats, etc.

XML encoding conventions

  • '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>'.
  • In map[string]interface{} values keys that are prepended by a hyphen, '-', are assumed to be attributes.

Caveat

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.

Documentation

http://godoc.org/github.com/clbanning/anyxml

Example

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>