Skip to content
XML to MAP converter written Golang
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore xml2map encoder Aug 6, 2018
.travis.yml Update .travis.yml Oct 17, 2018
LICENSE Create LICENSE Oct 12, 2018 Update Apr 23, 2019
decoder.go NewDecoderWithPrefix Jan 9, 2019
decoder_test.go NewDecoderWithPrefix Jan 9, 2019

Build Status Coverage Status Go Report Card GoDoc Awesome


XML to MAP converter written Golang

Sometimes there is a need for the representation of previously unknown structures. Such a universal representation is usually a string in the form of JSON, XML, or the structure of data map. similar to the map[string]interface{} or map[interface{}]interface{}.

This is a converter from the old XML format to map[string]interface{} Golang

For example, the map[string]interface{} can be used as a universal type in template generation. Golang "text/template" and etc.

Getting started

1. install

go get -u

Or, using dep:

dep ensure -add

2. use it

func main() {
	data := `<container uid="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
					<dog color="gray">hello</dog>

	decoder := xml2map.NewDecoder(strings.NewReader(data))
	result, err := decoder.Decode()

	if err != nil {
		fmt.Printf("%v\n", err)
	} else {
		fmt.Printf("%v\n", result)
	v := result["container"].
	fmt.Printf("n[1]: %v\n", v)

if you want to use your custom prefixes use the

NewDecoderWithPrefix(reader io.Reader, attrPrefix, textPrefix string) *Decoder


map[container:map[@uid:FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6 @lifetime:2019-10-10T18:00:11 cats:map[cat:[map[id:CDA035B6-D453-4A17-B090-84295AE2DEC5 name:moritz age:7 items:map[n:[1293 1255 1257]]] map[id:1634C644-975F-4302-8336-1EF1366EC6A4 name:oliver age:12]] dog:map[@color:gray #text:hello]] color:white city:NY]]

result: 1255


$ go test -bench=. -benchmem
goos: darwin
goarch: amd64
BenchmarkDecoder-8         50000             29773 ns/op           15032 B/op        261 allocs/op
ok       1.805s




Babiv Sergey

You can’t perform that action at this time.