x2j

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var X2jCharsetReader func(charset string, input io.Reader) (io.Reader, error)

If X2jCharsetReader != nil, it will be used to decode the doc or stream if required

import charset "code.google.com/p/go-charset/charset"
...
x2j.X2jCharsetReader = charset.NewReader
s, err := x2j.DocToJson(doc)

Functions

func ByteDocToJson

func ByteDocToJson(doc []byte, recast ...bool) (string, error)

ByteDocToJson - return an XML doc as a JSON string.

If the optional argument 'recast' is 'true', then values will be converted to boolean or float64 if possible.

func ByteDocToMap

func ByteDocToMap(doc []byte, recast ...bool) (map[string]interface{}, error)

ByteDocToMap - convert an XML doc into a map[string]interface{}. (This is analogous to unmarshalling a JSON string to map[string]interface{} using json.Unmarshal().)

If the optional argument 'recast' is 'true', then values will be converted to boolean or float64 if possible.
Note: recasting is only applied to element values, not attribute values.

func DocToJson

func DocToJson(doc string, recast ...bool) (string, error)

DocToJson - return an XML doc as a JSON string.

If the optional argument 'recast' is 'true', then values will be converted to boolean or float64 if possible.

func DocToJsonIndent

func DocToJsonIndent(doc string, recast ...bool) (string, error)

DocToJsonIndent - return an XML doc as a prettified JSON string.

If the optional argument 'recast' is 'true', then values will be converted to boolean or float64 if possible.
Note: recasting is only applied to element values, not attribute values.

func DocToMap

func DocToMap(doc string, recast ...bool) (map[string]interface{}, error)

DocToMap - convert an XML doc into a map[string]interface{}. (This is analogous to unmarshalling a JSON string to map[string]interface{} using json.Unmarshal().)

If the optional argument 'recast' is 'true', then values will be converted to boolean or float64 if possible.
Note: recasting is only applied to element values, not attribute values.

func DocValue

func DocValue(doc, path string, attrs ...string) (interface{}, error)

DocValue - return a value for a specific tag

'doc' is a valid XML message.
'path' is a hierarchy of XML tags, e.g., "doc.name".
'attrs' is an OPTIONAL list of "name:value" pairs for attributes.
Note: 'recast' is not enabled here. Use DocToMap(), NewAttributeMap(), and MapValue() calls for that.

func MapValue

func MapValue(m map[string]interface{}, path string, attr map[string]interface{}, r ...bool) (interface{}, error)

MapValue - retrieves value based on walking the map, 'm'.

'm' is the map value of interest.
'path' is a period-separated hierarchy of keys in the map.
'attr' is a map of attribute "name:value" pairs from NewAttributeMap().  May be 'nil'.
If the path can't be traversed, an error is returned.
Note: the optional argument 'r' can be used to coerce attribute values, 'attr', if done so for 'm'.

func NewAttributeMap

func NewAttributeMap(kv ...string) (map[string]interface{}, error)

NewAttributeMap() - generate map of attributes=value entries as map["-"+string]string.

'kv' arguments are "name:value" pairs that appear as attributes, name="value".
If len(kv) == 0, the return is (nil, nil).

func Unmarshal

func Unmarshal(doc []byte, v interface{}) error

Unmarshal - wraps xml.Unmarshal with handling of map[string]interface{} and string type variables.

Usage: x2j.Unmarshal(doc,&m) where m of type map[string]interface{}
       x2j.Unmarshal(doc,&s) where s of type string (Overrides xml.Unmarshal().)
       x2j.Unmarshal(doc,&struct) - passed to xml.Unmarshal()
       x2j.Unmarshal(doc,&slice) - passed to xml.Unmarshal()

func ValuesForKey

func ValuesForKey(m map[string]interface{}, key string) []interface{}

ValuesForKey - return all values in map associated with 'key'

Returns nil if the 'key' does not occur in the map

func ValuesForTag

func ValuesForTag(doc, tag string) ([]interface{}, error)

ValuesForTag - return all values in doc associated with 'tag'.

Returns nil if the 'tag' does not occur in the doc.
If there is an error encounted while parsing doc, that is returned.
If you want values 'recast' use DocToMap() and ValuesForKey().

func WriteMap

func WriteMap(m interface{}, offset ...int) string

WriteMap - dumps the map[string]interface{} for examination.

'offset' is initial indentation count; typically: WriteMap(m).
NOTE: with XML all element types are 'string'.
But code written as generic for use with maps[string]interface{} values from json.Unmarshal().
Or it can handle a DocToMap(doc,true) result where values have been recast'd.

Types

type Node

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

func ByteDocToTree

func ByteDocToTree(doc []byte) (*Node, error)

ByteDocToTree - convert an XML doc into a tree of nodes.

func DocToTree

func DocToTree(doc string) (*Node, error)

DocToTree - convert an XML doc into a tree of nodes.

func (*Node) WriteTree

func (n *Node) WriteTree(padding ...int) string

(*Node)WriteTree - convert a tree of nodes into a printable string.

'padding' is the starting indentation; typically: n.WriteTree().

Jump to

Keyboard shortcuts

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