Skip to content

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

License

Notifications You must be signed in to change notification settings

clbanning/anyxml

Repository files navigation

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

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>

An example of encoding a map with mixed value types is in anyxml/examples/goofy_map.go.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages