:github_url: hide .. _class_XMLParser: XMLParser ========= **Hérite de :** :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 Méthodes ---------------- .. 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 Énumérations ------------------------ .. _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`` Il y aucun nœud (pas de fichier ou de mémoire tampon ouverte). .. _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 Descriptions des méthodes -------------------------------------------------- .. _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:`🔗` Renvoie ``true`` si l’élément actuellement traité est vide, par exemple ````. .. 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 (Cette méthode doit typiquement être redéfinie par l'utilisateur pour avoir un effet.)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (Cette méthode n'a pas d'effets de bord. Elle ne modifie aucune des variables membres de l'instance.)` .. |vararg| replace:: :abbr:`vararg (Cette méthode accepte n'importe quel nombre d'arguments après ceux décris ici.)` .. |constructor| replace:: :abbr:`constructor (Cette méthode est utilisée pour construire un type.)` .. |static| replace:: :abbr:`static (Cette méthode n'a pas besoin d'instance pour être appelée, elle peut donc être directement appelée en utilisant le nom de la classe.)` .. |operator| replace:: :abbr:`operator (Cette méthode décrit un opérateur valide à utiliser avec ce type en tant qu'opérande gauche.)` .. |bitfield| replace:: :abbr:`BitField (Cette valeur est un nombre entier composé d'un masque de bits des options suivantes.)` .. |void| replace:: :abbr:`void (Aucune valeur de retour.)`