Table of Contents

Class XMLParser

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

Inheritance
XMLParser

Remarks

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 XMLParser.open method or a buffer with the XMLParser.open_buffer 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:

var parser = XMLParser.new()
parser.open("path/to/file.svg")
while parser.read() != ERR_FILE_EOF:
    if parser.get_node_type() == XMLParser.NODE_ELEMENT:
        var node_name = parser.get_node_name()
        var attributes_dict = {}
        for idx in range(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

get_attribute_count

Qualifiers: const

Returns the number of attributes in the currently parsed element.

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

int get_attribute_count

get_attribute_name(int)

Qualifiers: const

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

String get_attribute_name(int idx)

Parameters

idx int

get_attribute_value(int)

Qualifiers: const

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

String get_attribute_value(int idx)

Parameters

idx int

get_current_line

Qualifiers: const

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

int get_current_line

get_named_attribute_value(String)

Qualifiers: const

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.

String get_named_attribute_value(String name)

Parameters

name String

get_named_attribute_value_safe(String)

Qualifiers: const

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.

String get_named_attribute_value_safe(String name)

Parameters

name String

get_node_data

Qualifiers: const

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

String get_node_data

get_node_name

Qualifiers: 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 a XMLParser.NODE_CDATA node and the comment string of a XMLParser.NODE_COMMENT node are also considered names.

String get_node_name

get_node_offset

Qualifiers: 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.

int get_node_offset

get_node_type

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

int get_node_type

has_attribute(String)

Qualifiers: const

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

bool has_attribute(String name)

Parameters

name String

is_empty

Qualifiers: const

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

bool is_empty

open(String)

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

int open(String file)

Parameters

file String

open_buffer(PackedByteArray)

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

int open_buffer(PackedByteArray buffer)

Parameters

buffer PackedByteArray

read

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

int read

seek(int)

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

int seek(int position)

Parameters

position int

skip_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.

void skip_section