XMLParser

package
v0.0.0-...-e10d1cd Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Provides a low-level interface for creating parsers for XML files. This class can serve as base to make custom XML parsers.

To parse XML, you must open a file with the Open method or a buffer with the OpenBuffer method. Then, the Read method must be called to parse the next nodes. Most of the methods take into consideration the currently parsed node.

Here is an example of using XMLParser to parse an SVG file (which is based on XML), printing each element and its attributes as a dictionary:

package main

import "graphics.gd/classdb/XMLParser"

func ExampleXMLParser() {
	var parser = XMLParser.New()
	parser.Open("path/to/file.svg")
	for parser.Read() != nil {
		if parser.GetNodeType() == XMLParser.NodeElement {
			var nodeName = parser.GetNodeName()
			var attributesDict = make(map[string]string)
			for idx := 0; idx < parser.GetAttributeCount(); idx++ {
				attributesDict[parser.GetAttributeName(idx)] = parser.GetAttributeValue(idx)
			}
			print("The ", nodeName, " element has the following attributes: ", attributesDict)
		}
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsXMLParser() Instance
}

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

func (*Extension[T]) AsXMLParser

func (self *Extension[T]) AsXMLParser() Instance

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.XMLParser

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) AsXMLParser

func (self Instance) AsXMLParser() Instance

func (Instance) GetAttributeCount

func (self Instance) GetAttributeCount() int

Returns the number of attributes in the currently parsed element.

Note: If this method is used while the currently parsed node is not NodeElement or NodeElementEnd, this count will not be updated and will still reflect the last element.

func (Instance) GetAttributeName

func (self Instance) GetAttributeName(idx int) string

Returns the name of an attribute of the currently parsed element, specified by the 'idx' index.

func (Instance) GetAttributeValue

func (self Instance) GetAttributeValue(idx int) string

Returns the value of an attribute of the currently parsed element, specified by the 'idx' index.

func (Instance) GetCurrentLine

func (self Instance) GetCurrentLine() int

Returns the current line in the parsed file, counting from 0.

func (Instance) GetNamedAttributeValue

func (self Instance) GetNamedAttributeValue(name string) string

Returns the value of an attribute of the currently parsed element, specified by its 'name'. This method will raise an error if the element has no such attribute.

func (Instance) GetNamedAttributeValueSafe

func (self Instance) GetNamedAttributeValueSafe(name string) string

Returns the value of an attribute of the currently parsed element, specified by its 'name'. This method will return an empty string if the element has no such attribute.

func (Instance) GetNodeData

func (self Instance) GetNodeData() string

Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type.

func (Instance) GetNodeName

func (self Instance) GetNodeName() string

Returns the name of a node. This method will raise an error if the currently parsed node is a text node.

Note: The content of a NodeCdata node and the comment string of a NodeComment node are also considered names.

func (Instance) GetNodeOffset

func (self Instance) GetNodeOffset() int

Returns the byte offset of the currently parsed node since the beginning of the file or buffer. This is usually equivalent to the number of characters before the read position.

func (Instance) GetNodeType

func (self Instance) GetNodeType() NodeType

Returns the type of the current node. Compare with NodeType constants.

func (Instance) HasAttribute

func (self Instance) HasAttribute(name string) bool

Returns true if the currently parsed element has an attribute with the 'name'.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsEmpty

func (self Instance) IsEmpty() bool

Returns true if the currently parsed element is empty, e.g. <element />.

func (Instance) Open

func (self Instance) Open(file string) error

Opens an XML 'file' for parsing. This method returns an error code.

func (Instance) OpenBuffer

func (self Instance) OpenBuffer(buffer []byte) error

Opens an XML raw 'buffer' for parsing. This method returns an error code.

func (Instance) Read

func (self Instance) Read() error

Parses the next node in the file. This method returns an error code.

func (Instance) SeekTo

func (self Instance) SeekTo(position int) error

Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code.

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SkipSection

func (self Instance) SkipSection()

Skips the current section. If the currently parsed node contains more inner nodes, they will be ignored and the cursor will go to the closing of the current element.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type NodeType

type NodeType int //gd:XMLParser.NodeType
const (
	// There's no node (no file or buffer opened).
	NodeNone NodeType = 0
	// An element node type, also known as a tag, e.g. <title>.
	NodeElement NodeType = 1
	// An end of element node type, e.g. </title>.
	NodeElementEnd NodeType = 2
	// A text node type, i.e. text that is not inside an element. This includes whitespace.
	NodeText NodeType = 3
	// A comment node type, e.g. <!--A comment-->.
	NodeComment NodeType = 4
	// A node type for CDATA (Character Data) sections, e.g. <![CDATA[CDATA section]]>.
	NodeCdata NodeType = 5
	// An unknown node type.
	NodeUnknown NodeType = 6
)

Jump to

Keyboard shortcuts

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