Documentation ¶
Overview ¶
This package wraps the standard XML library and uses it to build a node tree of any document you load. This allows you to look up nodes forwards and backwards, as well as perform simple search queries.
Nodes now simply become collections and don't require you to read them in the order in which the xml.Parser finds them.
The Document currently implements 2 search functions which allow you to look for specific nodes.
*xmlx.Document.SelectNode(namespace, name string) *Node; *xmlx.Document.SelectNodes(namespace, name string) []*Node; *xmlx.Document.SelectNodesRecursive(namespace, name string) []*Node;
SelectNode() returns the first, single node it finds matching the given name and namespace. SelectNodes() returns a slice containing all the matching nodes (without recursing into matching nodes). SelectNodesRecursive() returns a slice of all matching nodes, including nodes inside other matching nodes.
Note that these search functions can be invoked on individual nodes as well. This allows you to search only a subset of the entire document.
Index ¶
- Constants
- Variables
- func EntityToUtf8(entity string) string
- func Utf8ToEntity(entity string) string
- type Attr
- type CharsetFunc
- type Document
- func (this *Document) LoadBytes(d []byte, charset CharsetFunc) (err error)
- func (this *Document) LoadExtendedEntityMap()
- func (this *Document) LoadFile(filename string, charset CharsetFunc) (err error)
- func (this *Document) LoadStream(r io.Reader, charset CharsetFunc) (err error)
- func (this *Document) LoadString(s string, charset CharsetFunc) (err error)
- func (this *Document) LoadUri(uri string, charset CharsetFunc) (err error)
- func (this *Document) LoadUriClient(uri string, client *http.Client, charset CharsetFunc) (err error)
- func (this *Document) SaveBytes() []byte
- func (this *Document) SaveFile(path string) error
- func (this *Document) SaveStream(w io.Writer) (err error)
- func (this *Document) SaveString() string
- func (this *Document) SelectNode(namespace, name string) *Node
- func (this *Document) SelectNodes(namespace, name string) []*Node
- func (this *Document) SelectNodesDirect(namespace, name string) []*Node
- func (this *Document) SelectNodesRecursive(namespace, name string) []*Node
- func (this *Document) SetUserAgent(s string)
- func (this *Document) String() string
- type Node
- func (this *Node) Ab(namespace, name string) bool
- func (this *Node) AddChild(t *Node)
- func (this *Node) Af32(namespace, name string) float32
- func (this *Node) Af64(namespace, name string) float64
- func (this *Node) Ai(namespace, name string) int
- func (this *Node) Ai16(namespace, name string) int16
- func (this *Node) Ai32(namespace, name string) int32
- func (this *Node) Ai64(namespace, name string) int64
- func (this *Node) Ai8(namespace, name string) int8
- func (this *Node) As(namespace, name string) string
- func (this *Node) Au(namespace, name string) uint
- func (this *Node) Au16(namespace, name string) uint16
- func (this *Node) Au32(namespace, name string) uint32
- func (this *Node) Au64(namespace, name string) uint64
- func (this *Node) Au8(namespace, name string) uint8
- func (this *Node) B(namespace, name string) bool
- func (this *Node) Bytes() []byte
- func (this *Node) F32(namespace, name string) float32
- func (this *Node) F64(namespace, name string) float64
- func (this *Node) GetValue() string
- func (this *Node) HasAttr(namespace, name string) bool
- func (this *Node) I(namespace, name string) int
- func (this *Node) I16(namespace, name string) int16
- func (this *Node) I32(namespace, name string) int32
- func (this *Node) I64(namespace, name string) int64
- func (this *Node) I8(namespace, name string) int8
- func (this *Node) RemoveAttr(name string)
- func (this *Node) RemoveChild(t *Node)
- func (this *Node) RemoveNameSpace()
- func (this *Node) S(namespace, name string) string
- func (this *Node) SelectNode(namespace, name string) *Node
- func (this *Node) SelectNodes(namespace, name string) []*Node
- func (this *Node) SelectNodesDirect(namespace, name string) []*Node
- func (this *Node) SelectNodesRecursive(namespace, name string) []*Node
- func (this *Node) SetAttr(name, value string)
- func (this *Node) SetValue(val string)
- func (this *Node) String() (s string)
- func (this *Node) U(namespace, name string) uint
- func (this *Node) U16(namespace, name string) uint16
- func (this *Node) U32(namespace, name string) uint32
- func (this *Node) U64(namespace, name string) uint64
- func (this *Node) U8(namespace, name string) uint8
- func (this *Node) Unmarshal(obj interface{}) error
Constants ¶
const ( NT_ROOT = iota NT_DIRECTIVE NT_PROCINST NT_COMMENT NT_TEXT NT_ELEMENT )
Variables ¶
var IndentPrefix = ""
IndentPrefix holds the value for a single identation level, if one chooses to want indentation in the node.String() and node.Bytes() output. This would normally be set to a single tab, or a number of spaces.
Functions ¶
func EntityToUtf8 ¶
Converts a single numerical html entity to a regular Go utf8-token.
func Utf8ToEntity ¶
Converts a single Go utf8-token to a Html entity.
Types ¶
type CharsetFunc ¶
This signature represents a character encoding conversion routine. Used to tell the xml decoder how to deal with non-utf8 characters.
type Document ¶
type Document struct { Version string // XML version Encoding string // Encoding found in document. If absent, assumes UTF-8. StandAlone string // Value of XML doctype's 'standalone' attribute. Entity map[string]string // Mapping of custom entity conversions. Root *Node // The document's root node. SaveDocType bool // Whether not to include the XML doctype in saves. // contains filtered or unexported fields }
represents a single XML document.
func (*Document) LoadBytes ¶
func (this *Document) LoadBytes(d []byte, charset CharsetFunc) (err error)
Load the contents of this document from the supplied byte slice.
func (*Document) LoadExtendedEntityMap ¶
func (this *Document) LoadExtendedEntityMap()
This loads a rather massive table of non-conventional xml escape sequences. Needed to make the parser map them to characters properly. It is advised to set only those entities needed manually using the document.Entity map, but if need be, this method can be called to fill the map with the entire set defined on http://www.w3.org/TR/html4/sgml/entities.html
func (*Document) LoadFile ¶
func (this *Document) LoadFile(filename string, charset CharsetFunc) (err error)
Load the contents of this document from the supplied file.
func (*Document) LoadStream ¶
func (this *Document) LoadStream(r io.Reader, charset CharsetFunc) (err error)
Load the contents of this document from the supplied reader.
func (*Document) LoadString ¶
func (this *Document) LoadString(s string, charset CharsetFunc) (err error)
Load the contents of this document from the supplied string.
func (*Document) LoadUri ¶
func (this *Document) LoadUri(uri string, charset CharsetFunc) (err error)
Load the contents of this document from the supplied uri. (calls LoadUriClient with http.DefaultClient)
func (*Document) LoadUriClient ¶
func (this *Document) LoadUriClient(uri string, client *http.Client, charset CharsetFunc) (err error)
Load the contents of this document from the supplied uri using the specifed client.
func (*Document) SaveStream ¶
Save the contents of this document to the supplied writer.
func (*Document) SaveString ¶
Save the contents of this document as a string.
func (*Document) SelectNode ¶
Select a single node with the given namespace and name. Returns nil if no matching node was found.
func (*Document) SelectNodes ¶
Select all nodes with the given namespace and name. Returns an empty slice if no matches were found. Select all nodes with the given namespace and name, without recursing into the children of those matches. Returns an empty slice if no matching node was found.
func (*Document) SelectNodesDirect ¶
Select all nodes directly under this document, with the given namespace and name. Returns an empty slice if no matches were found.
func (*Document) SelectNodesRecursive ¶
Select all nodes with the given namespace and name, also recursing into the children of those matches. Returns an empty slice if no matches were found.
func (*Document) SetUserAgent ¶
Set a custom user agent when making a new request.
type Node ¶
type Node struct { Type byte // Node type. Name xml.Name // Node namespace and name. Children []*Node // Child nodes. Attributes []*Attr // Node attributes. Parent *Node // Parent node. Value string // Node value. Target string // procinst field. }
func (*Node) Bytes ¶
Convert node to appropriate []byte representation based on it's @Type. Note that NT_ROOT is a special-case empty node used as the root for a Document. This one has no representation by itself. It merely forwards the String() call to it's child nodes.
func (*Node) RemoveAttr ¶
func (*Node) RemoveNameSpace ¶
func (this *Node) RemoveNameSpace()
func (*Node) SelectNode ¶
Select single node by name
func (*Node) SelectNodes ¶
Select multiple nodes by name
func (*Node) SelectNodesDirect ¶
Select multiple nodes directly under this node, by name.
func (*Node) SelectNodesRecursive ¶
Select multiple nodes by name
func (*Node) SetValue ¶
SetValue sets the value of the node to the given parameter. It deletes all children of the node so the old data does not get back at node.GetValue