:github_url: hide .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/4.3/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/4.3/doc/classes/XMLParser.xml. .. _class_XMLParser: XMLParser ========= **Inherits:** :ref:`RefCounted` **<** :ref:`Object` Provides a low-level interface for creating parsers for XML files. .. rst-class:: classref-introduction-group 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 :ref:`open` method or a buffer with the :ref:`open_buffer` method. Then, the :ref:`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: .. tabs:: .. code-tab:: gdscript 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) .. code-tab:: csharp var parser = new XmlParser(); parser.Open("path/to/file.svg"); while (parser.Read() != Error.FileEof) { if (parser.GetNodeType() == XmlParser.NodeType.Element) { var nodeName = parser.GetNodeName(); var attributesDict = new Godot.Collections.Dictionary(); for (int idx = 0; idx < parser.GetAttributeCount(); idx++) { attributesDict[parser.GetAttributeName(idx)] = parser.GetAttributeValue(idx); } GD.Print($"The {nodeName} element has the following attributes: {attributesDict}"); } } .. rst-class:: classref-reftable-group Methods ------- .. table:: :widths: auto +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_attribute_count`\ (\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_attribute_name`\ (\ idx\: :ref:`int`\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_attribute_value`\ (\ idx\: :ref:`int`\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_current_line`\ (\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_named_attribute_value`\ (\ name\: :ref:`String`\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_named_attribute_value_safe`\ (\ name\: :ref:`String`\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_node_data`\ (\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_node_name`\ (\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_node_offset`\ (\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`NodeType` | :ref:`get_node_type`\ (\ ) | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_attribute`\ (\ name\: :ref:`String`\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_empty`\ (\ ) |const| | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`open`\ (\ file\: :ref:`String`\ ) | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`open_buffer`\ (\ buffer\: :ref:`PackedByteArray`\ ) | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`read`\ (\ ) | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`seek`\ (\ position\: :ref:`int`\ ) | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`skip_section`\ (\ ) | +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Enumerations ------------ .. _enum_XMLParser_NodeType: .. rst-class:: classref-enumeration enum **NodeType**: :ref:`🔗` .. _class_XMLParser_constant_NODE_NONE: .. rst-class:: classref-enumeration-constant :ref:`NodeType` **NODE_NONE** = ``0`` There's no node (no file or buffer opened). .. _class_XMLParser_constant_NODE_ELEMENT: .. rst-class:: classref-enumeration-constant :ref:`NodeType` **NODE_ELEMENT** = ``1`` An element node type, also known as a tag, e.g. ````. .. _class_XMLParser_constant_NODE_ELEMENT_END: .. rst-class:: classref-enumeration-constant :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_ELEMENT_END** = ``2`` An end of element node type, e.g. ````. .. _class_XMLParser_constant_NODE_TEXT: .. rst-class:: classref-enumeration-constant :ref:`NodeType` **NODE_TEXT** = ``3`` A text node type, i.e. text that is not inside an element. This includes whitespace. .. _class_XMLParser_constant_NODE_COMMENT: .. rst-class:: classref-enumeration-constant :ref:`NodeType` **NODE_COMMENT** = ``4`` A comment node type, e.g. ````. .. _class_XMLParser_constant_NODE_CDATA: .. rst-class:: classref-enumeration-constant :ref:`NodeType` **NODE_CDATA** = ``5`` A node type for CDATA (Character Data) sections, e.g. ````. .. _class_XMLParser_constant_NODE_UNKNOWN: .. rst-class:: classref-enumeration-constant :ref:`NodeType` **NODE_UNKNOWN** = ``6`` An unknown node type. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Method Descriptions ------------------- .. _class_XMLParser_method_get_attribute_count: .. rst-class:: classref-method :ref:`int` **get_attribute_count**\ (\ ) |const| :ref:`🔗` Returns the number of attributes in the currently parsed element. \ **Note:** If this method is used while the currently parsed node is not :ref:`NODE_ELEMENT` or :ref:`NODE_ELEMENT_END`, this count will not be updated and will still reflect the last element. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_attribute_name: .. rst-class:: classref-method :ref:`String` **get_attribute_name**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns the name of an attribute of the currently parsed element, specified by the ``idx`` index. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_attribute_value: .. rst-class:: classref-method :ref:`String` **get_attribute_value**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns the value of an attribute of the currently parsed element, specified by the ``idx`` index. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_current_line: .. rst-class:: classref-method :ref:`int` **get_current_line**\ (\ ) |const| :ref:`🔗` Returns the current line in the parsed file, counting from 0. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_named_attribute_value: .. rst-class:: classref-method :ref:`String` **get_named_attribute_value**\ (\ name\: :ref:`String`\ ) |const| :ref:`🔗` 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. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_named_attribute_value_safe: .. rst-class:: classref-method :ref:`String` **get_named_attribute_value_safe**\ (\ name\: :ref:`String`\ ) |const| :ref:`🔗` 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. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_node_data: .. rst-class:: classref-method :ref:`String` **get_node_data**\ (\ ) |const| :ref:`🔗` Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_node_name: .. rst-class:: classref-method :ref:`String` **get_node_name**\ (\ ) |const| :ref:`🔗` 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 :ref:`NODE_CDATA` node and the comment string of a :ref:`NODE_COMMENT` node are also considered names. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_node_offset: .. rst-class:: classref-method :ref:`int` **get_node_offset**\ (\ ) |const| :ref:`🔗` 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. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_get_node_type: .. rst-class:: classref-method :ref:`NodeType` **get_node_type**\ (\ ) :ref:`🔗` Returns the type of the current node. Compare with :ref:`NodeType` constants. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_has_attribute: .. rst-class:: classref-method :ref:`bool` **has_attribute**\ (\ name\: :ref:`String`\ ) |const| :ref:`🔗` Returns ``true`` if the currently parsed element has an attribute with the ``name``. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_is_empty: .. rst-class:: classref-method :ref:`bool` **is_empty**\ (\ ) |const| :ref:`🔗` Returns ``true`` if the currently parsed element is empty, e.g. ````. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_open: .. rst-class:: classref-method :ref:`Error` **open**\ (\ file\: :ref:`String`\ ) :ref:`🔗` Opens an XML ``file`` for parsing. This method returns an error code. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_open_buffer: .. rst-class:: classref-method :ref:`Error` **open_buffer**\ (\ buffer\: :ref:`PackedByteArray`\ ) :ref:`🔗` Opens an XML raw ``buffer`` for parsing. This method returns an error code. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_read: .. rst-class:: classref-method :ref:`Error` **read**\ (\ ) :ref:`🔗` Parses the next node in the file. This method returns an error code. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_seek: .. rst-class:: classref-method :ref:`Error` **seek**\ (\ position\: :ref:`int`\ ) :ref:`🔗` Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code. .. rst-class:: classref-item-separator ---- .. _class_XMLParser_method_skip_section: .. rst-class:: classref-method |void| **skip_section**\ (\ ) :ref:`🔗` 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. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)` .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)` .. |void| replace:: :abbr:`void (No return value.)`