Documentation
¶
Overview ¶
Package xmlx extends the features of the encoding/xml package with a generic unmarshallable xml structure.
It provides a new structure, Node, which can unmarshal any xml data. This node has two useful methods: Map and Split.
The Map method returns a map of name, data, attributes and subnodes to their values.
The Split method returns an array of nodes having the same property as the parent, splitted after a subnode name.
Index ¶
- func Chunk(reader io.ReadSeeker, token string, offset int64) ([2]int64, error)
- func ChunkAll(reader io.ReadSeeker, token string, bulkLen int) ([][2]int64, error)
- type Node
- func (p Node) FindNode(label string) *Node
- func (p Node) FindNodeRecursively(label string) Node
- func (n Node) GetSubNode(labelsA ...string) *Node
- func (n Node) GetSubNodeBy(labelA string, subLabelA string, valueA string) *Node
- func (n Node) GetSubNodeBy2(labelA string, subLabel1A string, value1A string, subLabel2A string, ...) *Node
- func (n Node) GetSubNodeByX(rootLabesA string, labelA string, labelValuePairA ...string) *Node
- func (n Node) GetSubNodeString(labelsA ...string) string
- func (n Node) GetSubNodeStringBy(labelA string, subLabel1A string, value1A string, subLabel2A string) string
- func (n Node) GetSubNodeStringBy2(labelA string, subLabel1A string, value1A string, subLabel2A string, ...) string
- func (n Node) GetSubNodeStringByX(rootLabesA string, labelA string, subLabelA string, labelValuePairA ...string) string
- func (n Node) GetSubNodeStringX(labelsA string) string
- func (n Node) GetSubNodeX(labelsA string) *Node
- func (p Node) IsValid() bool
- func (n Node) Map() map[string]string
- func (n Node) Split(label string) []Node
- func (n Node) SubNodes(labelA string) []Node
- func (p Node) Text() string
- func (n *Node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Node ¶
type Node struct { // The name of the node Name string // The attribute list of the node Attrs map[string]string // The data located within the node Data string // The subnodes within the node Nodes []Node IsInvalid bool // contains filtered or unexported fields }
Node is a generic XML node to parse .
Example ¶
input := `<music> <album> <songs> <song> <name>Don't Tread on Me</name> <number>6</number> </song> <song> <name>Through the Never</name> <number>7</number> </song> </songs> </album> </music>` var node Node err := xml.Unmarshal([]byte(input), &node) if err != nil { // do stuff... } fmt.Println(node) for _, n := range node.Split("album.songs") { fmt.Println(n) }
Output: {music map[] [{album map[] [{songs map[] [{song map[] [{name map[] Don't Tread on Me [] } {number map[] 6 [] }] } {song map[] [{name map[] Through the Never [] } {number map[] 7 [] }] }] }] }] } {music map[] [{songs map[] [{name map[] Don't Tread on Me [] } {number map[] 6 [] }] }] } {music map[] [{songs map[] [{name map[] Through the Never [] } {number map[] 7 [] }] }] }
func (Node) FindNodeRecursively ¶
func (Node) GetSubNode ¶
func (Node) GetSubNodeBy ¶
GetSubNodeBy get a sub-node value of a node, while one other sub-node has the proper value
func (Node) GetSubNodeBy2 ¶
func (n Node) GetSubNodeBy2(labelA string, subLabel1A string, value1A string, subLabel2A string, value2A string) *Node
GetSubNodeBy2 get a sub-node value of a node, while two other sub-nodes have the proper value
func (Node) GetSubNodeByX ¶
func (Node) GetSubNodeString ¶
GetSubNodeString default/error will return empty
func (Node) GetSubNodeStringBy ¶
func (n Node) GetSubNodeStringBy(labelA string, subLabel1A string, value1A string, subLabel2A string) string
GetSubNodeStringBy if 1 sub-node has proper value, return the other sub-nodes' value
func (Node) GetSubNodeStringBy2 ¶
func (n Node) GetSubNodeStringBy2(labelA string, subLabel1A string, value1A string, subLabel2A string, value2A string, subLabel3A string) string
GetSubNodeStringBy2 if 2 sub-nodes have proper values, return the other sub-nodes' value
func (Node) GetSubNodeStringByX ¶
func (Node) GetSubNodeStringX ¶
func (Node) GetSubNodeX ¶
func (Node) Map ¶
Map returns a flatten representation of the node. If a node contains nodes having the same name, only the last node will exist in the map.
func (Node) Split ¶
Split the node into many: each time the split label is encountered within a subnode of the node, a new node is created.
func (*Node) UnmarshalXML ¶
UnmarshalXML takes the content of an XML node and puts it into the Node structure.