Skip to content

DirectX/goxml2json

 
 

Repository files navigation

goxml2json Build Status

Go package that converts XML to JSON

Install

go get -u github.com/basgys/goxml2json

Importing

import github.com/basgys/goxml2json

Usage

Code example

  package main

  import (
  	"fmt"
  	"strings"

  	xj "github.com/basgys/goxml2json"
  )

  func main() {
  	// xml is an io.Reader
  	xml := strings.NewReader(`<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>`)
  	json, err := xj.Convert(xml)
  	if err != nil {
  		panic("That's embarrassing...")
  	}

  	fmt.Println(json.String())
  	// {"hello": "world"}
  }

Input

  <?xml version="1.0" encoding="UTF-8"?>
  <osm version="0.6" generator="CGImap 0.0.2">
   <bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
   <foo>bar</foo>
  </osm>

Output

  {
    "osm": {
      "-version": "0.6",
      "-generator": "CGImap 0.0.2",
      "bounds": {
        "-minlat": "54.0889580",
        "-minlon": "12.2487570",
        "-maxlat": "54.0913900",
        "-maxlon": "12.2524800"
      },
      "foo": "bar"
    }
  }

XML Attributes mapping

By default all attributes are being decorated on decoding using "-" prefix. Thus this example XML

<?xml version="1.0" encoding="UTF-8"?>
<hello foo="bar">
  <whole>world</whole>
</hello>

will be transformed to

{ "hello": { "-foo": "bar", "whole": "world" } }

To override this behaviour function

json, err := xml2json.ConvertWithAttrPrefix(xml, "")

can be used instead.

{ "hello": { "foo": "bar", "whole": "world" } }

Disclaimer

This project has been hacked in a few hours and is definitely not production ready.

Contributing

Feel free to contribute to this project if you want to fix/extend/improve it.

Contributors

TODO

  • Extract data types in JSON (numbers, boolean, ...)
  • Categorise errors
  • Option to prettify the JSON output
  • Benchmark

About

XML to JSON converter written in Golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%