xml2json

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2017 License: MIT Imports: 6 Imported by: 175 modules, 182 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goxml2json CircleCI

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"
    }
  }
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

Documentation

Overview

Package xml2json is an XML to JSON converter

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(r io.Reader) (*bytes.Buffer, error)

Convert converts the given XML document to JSON

Types

type Decoder

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

A Decoder reads and decodes XML objects from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(root *Node) error

Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.

func (*Decoder) DecodeWithCustomPrefixes added in v1.1.0

func (dec *Decoder) DecodeWithCustomPrefixes(root *Node, contentPrefix string, attributePrefix string) error

func (*Decoder) SetAttributePrefix added in v1.1.0

func (dec *Decoder) SetAttributePrefix(prefix string)

func (*Decoder) SetContentPrefix added in v1.1.0

func (dec *Decoder) SetContentPrefix(prefix string)

type Encoder

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

An Encoder writes JSON objects to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (enc *Encoder) Encode(root *Node) error

Encode writes the JSON encoding of v to the stream

func (*Encoder) EncodeWithCustomPrefixes added in v1.1.0

func (enc *Encoder) EncodeWithCustomPrefixes(root *Node, contentPrefix string, attributePrefix string) error

func (*Encoder) SetAttributePrefix added in v1.1.0

func (enc *Encoder) SetAttributePrefix(prefix string)

func (*Encoder) SetContentPrefix added in v1.1.0

func (enc *Encoder) SetContentPrefix(prefix string)

type Node

type Node struct {
	Children map[string]Nodes
	Data     string
}

Node is a data element on a tree

func (*Node) AddChild

func (n *Node) AddChild(s string, c *Node)

AddChild appends a node to the list of children

func (*Node) IsComplex

func (n *Node) IsComplex() bool

IsComplex returns whether it is a complex type (has children)

type Nodes

type Nodes []*Node

Nodes is a list of nodes

Jump to

Keyboard shortcuts

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