xmlx

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 6 Imported by: 2

README

xmlx

a modified version of github.com/moxar/xmlx, thanks.

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

func Chunk

func Chunk(reader io.ReadSeeker, token string, offset int64) ([2]int64, error)

Chunk returns the start and stop position of the first encountered token.

func ChunkAll

func ChunkAll(reader io.ReadSeeker, token string, bulkLen int) ([][2]int64, error)

ChunkAll reads the reader and defines a list of segments that correspond to valid chunks. Each segment, except for the last one, contains a number of chunks superior or equal to the provided bulkLen value.

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 GetXMLNode

func GetXMLNode(xmlA string, labelsA ...string) (*Node, error)

func (Node) FindNode

func (p Node) FindNode(label string) *Node

func (Node) FindNodeRecursively

func (p Node) FindNodeRecursively(label string) Node

func (Node) GetSubNode

func (n Node) GetSubNode(labelsA ...string) *Node

func (Node) GetSubNodeBy

func (n Node) GetSubNodeBy(labelA string, subLabelA string, valueA string) *Node

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 (n Node) GetSubNodeByX(rootLabesA string, labelA string, labelValuePairA ...string) *Node

func (Node) GetSubNodeString

func (n Node) GetSubNodeString(labelsA ...string) string

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 (n Node) GetSubNodeStringByX(rootLabesA string, labelA string, subLabelA string, labelValuePairA ...string) string

func (Node) GetSubNodeStringX

func (n Node) GetSubNodeStringX(labelsA string) string

func (Node) GetSubNodeX

func (n Node) GetSubNodeX(labelsA string) *Node

func (Node) IsValid

func (p Node) IsValid() bool

func (Node) Map

func (n Node) Map() map[string]string

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

func (n Node) Split(label string) []Node

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) SubNodes

func (n Node) SubNodes(labelA string) []Node

func (Node) Text

func (p Node) Text() string

func (*Node) UnmarshalXML

func (n *Node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML takes the content of an XML node and puts it into the Node structure.

Jump to

Keyboard shortcuts

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