xmlParser

package
v0.0.0-...-ecb25c3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type XMLNode

type XMLNode struct {
	Value interface{}
	Path  string
}

XMLNode is wrapper to mxj map. It can traverse the xml to get the data. NOTE:

All attributes will also become a node with key '-attributesName'.
And tags with attributes, their value will become a node with key '#text'.

Ex:

<ProductName sku="ABC">
	This will become node also.
</ProductName>

Will become:

map[string]interface{
  "-sku": "ABC",
  "#text": "This will become node also.",
}

func GenerateXMLNode

func GenerateXMLNode(xmlBuffer []byte) (*XMLNode, error)

GenerateXMLNode generate an XMLNode object from the xml response body.

func (*XMLNode) CurrentKey

func (xn *XMLNode) CurrentKey() string

CurrentKey return the key (tag) for the current node. Root node has empty key.

func (*XMLNode) Elements

func (xn *XMLNode) Elements() []string

Elements return the keys of immediate sub-elements of the current node.

func (*XMLNode) FindByFullPath

func (xn *XMLNode) FindByFullPath(path string) []XMLNode

FindByFullPath get the element data by absolute path. Path is separated by '.', ex: "Tag1.Tag2.Tag3". If current node have path "A.B.C", and query path is "A.B.C.D.E",

then the method will search elements in current node with path "D.E".

The method return a list XMLNode, each node represents all the sub-elements

of the match key. The nodes then can be use to traverse deeper individually.

func (*XMLNode) FindByKey

func (xn *XMLNode) FindByKey(key string) []XMLNode

FindByKey get the element data by any key (tag) in any depth. The method return a list XMLNode, each node represents all the sub-elements

of the match key. The nodes then can be use to traverse deeper individually.

func (*XMLNode) FindByKeys

func (xn *XMLNode) FindByKeys(keys ...string) []XMLNode

FindByKeys get the element data by any keys (tag) in any depth. Subsequential key need to be child of previous. The method return a list XMLNode, each node represents all the sub-elements

of the match key. The nodes then can be use to traverse deeper individually.

Ex:

If current node have path "A.B.C.D.E" and "A.B2.C2.D2.E",
then node.FindByKeys("B", "E") will return nodes E under first path.

func (*XMLNode) FindByPath

func (xn *XMLNode) FindByPath(path string) []XMLNode

FindByPath get the element data by path string. Path is relative to the current XMLNode. Path need to be start with direct sub node of current node. Path is separated by '.', ex: "Tag1.Tag2.Tag3". If current node is "A", and has sub path "B.C", then query on "B.C" will

return node C. But Query on "C" will return empty nodes.

The method return a list XMLNode, each node represents all the sub-elements

of the match key. The nodes then can be use to traverse deeper individually.

func (*XMLNode) IsLeaf

func (xn *XMLNode) IsLeaf() bool

IsLeaf check whether or not the current node is a leaf node.

func (*XMLNode) LeafNodes

func (xn *XMLNode) LeafNodes() []XMLNode

LeafNodes return all the leaf nodes of current node.

func (*XMLNode) LeafPaths

func (xn *XMLNode) LeafPaths() []string

LeafPaths return the path to the leaf nodes.

func (*XMLNode) PrintXML

func (xn *XMLNode) PrintXML()

PrintXML print the xml with two space indention.

func (*XMLNode) ToBool

func (xn *XMLNode) ToBool() (bool, error)

ToBool convert the node value to bool. If value is not valid bool, an error will be returned.

func (*XMLNode) ToFloat64

func (xn *XMLNode) ToFloat64() (float64, error)

ToFloat64 convert the node value to float64. If value is not valid float64, an error will be returned.

func (*XMLNode) ToInt

func (xn *XMLNode) ToInt() (int, error)

ToInt convert the node value to int. If value is not valid int, an error will be returned.

func (*XMLNode) ToMap

func (xn *XMLNode) ToMap() (mxj.Map, error)

ToMap convert the node value to a mxj map. If fail to convert, an error will be returned. Tags have no sub tag, but have attributes is also a map. Attributes of the tag has key '-attributesName'. Tags' value has key '#test'. Ex:

<MessageId MarketplaceID="ATVPDKDDIKX0D" SKU="24478624">
	173964729
</MessageId>

After to map,

 	map[string]string{
 		"-MarketplaceID": "ATVPDKDDIKX0D",
 		"-SKU": "24478624",
 		"#text": "173964729",
	}

func (*XMLNode) ToString

func (xn *XMLNode) ToString() (string, error)

ToString convert the node value to string. If value is not valid string, an error will be returned.

func (*XMLNode) ToStruct

func (xn *XMLNode) ToStruct(structPtr interface{}) error

ToStruct unmarshal the node value to struct. If value can not be unmarshal, an error will returned. ToStruct use json tag to unmarshal the map. Ex: To unmarshal the tag:

<MessageId MarketplaceID="ATVPDKDDIKX0D" SKU="24478624">
	173964729
</MessageId>

Can use struct:

msgID := struct {
	MarketplaceID string `json:"-MarketplaceID"`
	SKU           string `json:"-SKU"`
	ID            string `json:"#text"`
}{}

func (*XMLNode) ToTime

func (xn *XMLNode) ToTime() (time.Time, error)

ToTime convert the node value to timestamp. If value is not valid timestamp, an error will be returned.

func (*XMLNode) ValueType

func (xn *XMLNode) ValueType() reflect.Kind

ValueType return the type of node value.

func (*XMLNode) XML

func (xn *XMLNode) XML() ([]byte, error)

XML return the raw xml data. If current node has key, use it as root node tag. If current node doest has key, and only have one child node, then the child

node's key will become root node tag.

If current node doest has key, and have more than one child node, then

the a default tag <doc> will use as root tag.

Jump to

Keyboard shortcuts

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