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 ¶
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 // contains filtered or unexported fields }
Node is a generic XML node.
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) 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.