xml2json

package module
v1.5.7 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 9 Imported by: 0

README

goxml2json

Go package that converts XML to JSON

Identitii fork

Supports:

  • xmlns prefixes
  • maintains ordering

Original Repo

Install
go get -u github.com/ad3n/goxml2json
Importing
import github.com/ad3n/goxml2json
Usage

Code example

  package main

  import (
  	"fmt"
  	"strings"

  	xj "github.com/ad3n/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"
    }
  }

With type conversion

  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"?><price>19.95</price>`)
  	json, err := xj.Convert(xml, xj.WithTypeConverter(xj.Float))
  	if err != nil {
  		panic("That's embarrassing...")
  	}

  	fmt.Println(json.String())
  	// {"price": 19.95}
  }

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, ps ...plugin) (*bytes.Buffer, error)

func ExcludeAttributes

func ExcludeAttributes(attrs []string) *excluder

func IncludeNSPrefix

func IncludeNSPrefix(v bool) *includeNSPrefix

func IncludeXMLSequence

func IncludeXMLSequence(v bool) *includeXMLSequence

func NodePlugin

func NodePlugin(path string, plugin nodePlugin) nodeFormatter

func ToArray

func ToArray() *arrayFormatter

func WithAttrPrefix

func WithAttrPrefix(prefix string) *attrPrefixer

func WithContentPrefix

func WithContentPrefix(prefix string) *contentPrefixer

func WithNodes

func WithNodes(n ...nodeFormatter) *nodesFormatter

func WithTypeConverter

func WithTypeConverter(ts ...JSType) *customTypeConverter

Types

type Decoder

type Decoder struct {
	NameSpacePrefix map[string]string
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader, plugins ...plugin) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) AddFormatters

func (dec *Decoder) AddFormatters(formatters []nodeFormatter)

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

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

func (*Decoder) ExcludeAttributes

func (dec *Decoder) ExcludeAttributes(attrs []string)

func (*Decoder) IncludeXMLSequence

func (dec *Decoder) IncludeXMLSequence(v bool)

func (*Decoder) SetAttributePrefix

func (dec *Decoder) SetAttributePrefix(prefix string)

func (*Decoder) SetContentPrefix

func (dec *Decoder) SetContentPrefix(prefix string)

func (*Decoder) SetIncludePrefix

func (dec *Decoder) SetIncludePrefix(prefix string)

func (*Decoder) SetSequencePrefix

func (dec *Decoder) SetSequencePrefix(prefix string)

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer, plugins ...plugin) *Encoder

func (*Encoder) Encode

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

type JSType

type JSType int

https://cswr.github.io/JsonSchema/spec/basic_types/ JSType is a JavaScript extracted from a string

const (
	Bool JSType = iota
	Int
	Float
	String
	Null
)

func Str2JSType

func Str2JSType(s string) JSType

Str2JSType extract a JavaScript type from a string

type Node

type Node struct {
	Children              map[string]Nodes
	Data                  string
	ChildrenAlwaysAsArray bool
	Prefix                string
	ChildrenKeys          []string
}

Node is a data element on a tree

func (*Node) AddChild

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

func (*Node) AddNamespacePrefix

func (n *Node) AddNamespacePrefix(prefix string)

func (*Node) GetChild

func (n *Node) GetChild(path string) *Node

func (*Node) IsComplex

func (n *Node) IsComplex() bool

type Nodes

type Nodes []*Node

Jump to

Keyboard shortcuts

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