XMLParser

Inherits:RefCounted<Object

Provides a low-level interface for creating parsers for XML files.

Description

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

To parse XML, you must open a file with theopen() method or a buffer with theopen_buffer() method. Then, theread() 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 usingXMLParser to parse an SVG file (which is based on XML), printing each element and its attributes as a dictionary:

varparser=XMLParser.new()parser.open("path/to/file.svg")whileparser.read()!=ERR_FILE_EOF:ifparser.get_node_type()==XMLParser.NODE_ELEMENT:varnode_name=parser.get_node_name()varattributes_dict={}foridxinrange(parser.get_attribute_count()):attributes_dict[parser.get_attribute_name(idx)]=parser.get_attribute_value(idx)print("The ",node_name," element has the following attributes: ",attributes_dict)

Methods

int

get_attribute_count()const

String

get_attribute_name(idx:int)const

String

get_attribute_value(idx:int)const

int

get_current_line()const

String

get_named_attribute_value(name:String)const

String

get_named_attribute_value_safe(name:String)const

String

get_node_data()const

String

get_node_name()const

int

get_node_offset()const

NodeType

get_node_type()

bool

has_attribute(name:String)const

bool

is_empty()const

Error

open(file:String)

Error

open_buffer(buffer:PackedByteArray)

Error

read()

Error

seek(position:int)

void

skip_section()


Enumerations

enumNodeType:🔗

NodeTypeNODE_NONE =0

There's no node (no file or buffer opened).

NodeTypeNODE_ELEMENT =1

An element node type, also known as a tag, e.g.<title>.

NodeTypeNODE_ELEMENT_END =2

An end of element node type, e.g.</title>.

NodeTypeNODE_TEXT =3

A text node type, i.e. text that is not inside an element. This includes whitespace.

NodeTypeNODE_COMMENT =4

A comment node type, e.g.<!--Acomment-->.

NodeTypeNODE_CDATA =5

A node type for CDATA (Character Data) sections, e.g.<![CDATA[CDATAsection]]>.

NodeTypeNODE_UNKNOWN =6

An unknown node type.


Method Descriptions

intget_attribute_count()const🔗

Returns the number of attributes in the currently parsed element.

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


Stringget_attribute_name(idx:int)const🔗

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


Stringget_attribute_value(idx:int)const🔗

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


intget_current_line()const🔗

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


Stringget_named_attribute_value(name:String)const🔗

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


Stringget_named_attribute_value_safe(name:String)const🔗

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


Stringget_node_data()const🔗

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


Stringget_node_name()const🔗

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 aNODE_CDATA node and the comment string of aNODE_COMMENT node are also considered names.


intget_node_offset()const🔗

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.


NodeTypeget_node_type()🔗

Returns the type of the current node. Compare withNodeType constants.


boolhas_attribute(name:String)const🔗

Returnstrue if the currently parsed element has an attribute with thename.


boolis_empty()const🔗

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


Erroropen(file:String)🔗

Opens an XMLfile for parsing. This method returns an error code.


Erroropen_buffer(buffer:PackedByteArray)🔗

Opens an XML rawbuffer for parsing. This method returns an error code.


Errorread()🔗

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


Errorseek(position:int)🔗

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


voidskip_section()🔗

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.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.