Index · Object → RefCounted → XMLParser

XMLParser

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

Class Description

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

func get_attribute_count() -> int

Returns the number of attributes in the currently parsed element.

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

func get_attribute_name(idx: int) -> String

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

func get_attribute_value(idx: int) -> String

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

func get_current_line() -> int

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

func get_named_attribute_value(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 get_named_attribute_value_safe(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 get_node_data() -> 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 get_node_name() -> 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 NODE_CDATA node and the comment string of a NODE_COMMENT node are also considered names.

func get_node_offset() -> 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 get_node_type() -> XMLParser.NodeType

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

func has_attribute(name: String) -> bool

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

func is_empty() -> bool

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

func open(file: String) -> Error

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

func open_buffer(buffer: PackedByteArray) -> Error

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

func read() -> Error

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

func seek(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 skip_section() -> void

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.

Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Object (2)

Enums & Constants

enum NodeType
NODE_NONE = 0

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

NODE_ELEMENT = 1

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

NODE_ELEMENT_END = 2

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

NODE_TEXT = 3

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

NODE_COMMENT = 4

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

NODE_CDATA = 5

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

NODE_UNKNOWN = 6

An unknown node type.