xmltree

package module
v0.0.0-...-7195f31 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2022 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(root *Node, fn WalkFunc) error

Walk traverses deep-first the given xml tree. On each traversed node, it calls the WalkFunc. If the WalkFunc returns an error, the traversal stops and Walk returns the wrapped reported error back.

Types

type Node

type Node struct {
	Nodes  []*Node
	Token  xml.Token
	Parent *Node
}

Node represents a XML tree, which can have subnodes and connets it to its parent node.

If the Token is of type xml.StartElement, it can have Nodes (children) of other nodes with different types, but has to end with a node of type xml.EndElement. Example:

Token: xml.StartElement
Nodes: [xml.CharData, xml.Comment, xml.StartElement, xml.Comment, xml.EndElement]
                                   \
                                   \Token: xml.StartElement
                                   \Nodes: [xml.CharData, xml.EndElement]

This creates a tree like this:

CharData
Comment
StartNode A
  CharData
  StartNode B
    CharData
    EndNode B
  CharData
  EndNode A
CharData

func GetParent

func GetParent(child *Node, localName string) *Node

GetParent returns the first parent node of the given child, having the given localName.

func Parse

func Parse(data []byte) (*Node, error)

Parse translates the given xml document to a XML tree.

func (*Node) Append

func (n *Node) Append(tok xml.Token) *Node

Append adds the given token to the node childrens list, returning the newly appended child.

func (*Node) Copy

func (n *Node) Copy(parent *Node) *Node

Copy deep copies the node, anchoring at the new given parent.

func (*Node) Dump

func (n *Node) Dump() string

Dump returns the node structure in a human readable format. The format can change between versions.

func (*Node) MarshalXML

func (n *Node) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements the xml.Marshaler interface, so Node objects can be marshalled conveniently with `xml.Marshal(node)`. The output of marshal should result semantically to the originally parsed document.

type WalkFunc

type WalkFunc func(node *Node, depth uint) error

WalkFunc defines the function that can be called by a walk function on a xml tree.

Jump to

Keyboard shortcuts

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