xml2map

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2021 License: MIT Imports: 6 Imported by: 8

README

Build Status Coverage Status Go Report Card GoDoc Awesome

xml2map

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 github.com/sbabiv/xml2map

Or, using dep:

dep ensure -add github.com/sbabiv/xml2map
2. use it

func main() {
	data := `<container uid="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
				<cats>
					<cat>
						<id>CDA035B6-D453-4A17-B090-84295AE2DEC5</id>
						<name>moritz</name>
						<age>7</age> 	
						<items>
							<n>1293</n>
							<n>1255</n>
							<n>1257</n>
						</items>
					</cat>
					<cat>
						<id>1634C644-975F-4302-8336-1EF1366EC6A4</id>
						<name>oliver</name>
						<age>12</age>
					</cat>
					<dog color="gray">hello</dog>
				</cats>
				<color>white</color>
				<city>NY</city>
			</container>`

	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"].
		(map[string]interface{})["cats"].
			(map[string]interface{})["cat"].
				([]map[string]interface{})[0]["items"].
					(map[string]interface{})["n"].([]string)[1]
					
	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

Go Playground

Output

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

Benchmark

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

Licence

MIT

Author

Babiv Sergey

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidDocument invalid document err
	ErrInvalidDocument = errors.New("invalid document")

	//ErrInvalidRoot data at the root level is invalid err
	ErrInvalidRoot = errors.New("data at the root level is invalid")
)

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder instance

func NewDecoder

func NewDecoder(reader io.Reader) *Decoder

NewDecoder create new decoder instance

func NewDecoderWithPrefix

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

NewDecoderWithPrefix create new decoder instance with custom attribute prefix and text prefix

func (*Decoder) Decode

func (d *Decoder) Decode() (map[string]interface{}, error)

Decode xml string to map[string]interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL