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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0f0a447 · Feb 2, 2021

History

57 Commits
Aug 28, 2020
Feb 2, 2021
Feb 2, 2021
Mar 23, 2018
May 23, 2019
Jan 14, 2019
Feb 2, 2021
Feb 2, 2021
Jul 27, 2018
Feb 2, 2021
Feb 2, 2021
Oct 24, 2016
Jul 26, 2018

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>'.
  • 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>

An example of encoding a map with mixed key 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