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 ¶
- type Advanced
- type Any
- type Extension
- type ID
- type Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsXMLParser() Instance
- func (self Instance) GetAttributeCount() int
- func (self Instance) GetAttributeName(idx int) string
- func (self Instance) GetAttributeValue(idx int) string
- func (self Instance) GetCurrentLine() int
- func (self Instance) GetNamedAttributeValue(name string) string
- func (self Instance) GetNamedAttributeValueSafe(name string) string
- func (self Instance) GetNodeData() string
- func (self Instance) GetNodeName() string
- func (self Instance) GetNodeOffset() int
- func (self Instance) GetNodeType() NodeType
- func (self Instance) HasAttribute(name string) bool
- func (self Instance) ID() ID
- func (self Instance) IsEmpty() bool
- func (self Instance) Open(file string) error
- func (self Instance) OpenBuffer(buffer []byte) error
- func (self Instance) Read() error
- func (self Instance) SeekTo(position int) error
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SkipSection()
- func (self Instance) Virtual(name string) reflect.Value
- type NodeType
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 Extension ¶
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]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsXMLParser ¶
type 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.
type Instance ¶
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 (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsXMLParser ¶
func (Instance) GetAttributeCount ¶
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 ¶
Returns the name of an attribute of the currently parsed element, specified by the 'idx' index.
func (Instance) GetAttributeValue ¶
Returns the value of an attribute of the currently parsed element, specified by the 'idx' index.
func (Instance) GetCurrentLine ¶
Returns the current line in the parsed file, counting from 0.
func (Instance) GetNamedAttributeValue ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Returns the type of the current node. Compare with NodeType constants.
func (Instance) HasAttribute ¶
Returns true if the currently parsed element has an attribute with the 'name'.
func (Instance) OpenBuffer ¶
Opens an XML raw 'buffer' for parsing. This method returns an error code.
func (Instance) SeekTo ¶
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) 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.
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 )