mirror of
https://github.com/godotengine/godot-docs.git
synced 2025-12-31 17:49:03 +03:00
400 lines
18 KiB
ReStructuredText
400 lines
18 KiB
ReStructuredText
:github_url: hide
|
|
|
|
.. DO NOT EDIT THIS FILE!!!
|
|
.. Generated automatically from Godot engine sources.
|
|
.. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
|
|
.. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/XMLParser.xml.
|
|
|
|
.. _class_XMLParser:
|
|
|
|
XMLParser
|
|
=========
|
|
|
|
**Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_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 <https://en.wikipedia.org/wiki/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()<class_XMLParser_method_open>` method or a buffer with the :ref:`open_buffer()<class_XMLParser_method_open_buffer>` method. Then, the :ref:`read()<class_XMLParser_method_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<class_int>` | :ref:`get_attribute_count<class_XMLParser_method_get_attribute_count>`\ (\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_attribute_name<class_XMLParser_method_get_attribute_name>`\ (\ idx\: :ref:`int<class_int>`\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_attribute_value<class_XMLParser_method_get_attribute_value>`\ (\ idx\: :ref:`int<class_int>`\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`int<class_int>` | :ref:`get_current_line<class_XMLParser_method_get_current_line>`\ (\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_named_attribute_value<class_XMLParser_method_get_named_attribute_value>`\ (\ name\: :ref:`String<class_String>`\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_named_attribute_value_safe<class_XMLParser_method_get_named_attribute_value_safe>`\ (\ name\: :ref:`String<class_String>`\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_node_data<class_XMLParser_method_get_node_data>`\ (\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_node_name<class_XMLParser_method_get_node_name>`\ (\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`int<class_int>` | :ref:`get_node_offset<class_XMLParser_method_get_node_offset>`\ (\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`NodeType<enum_XMLParser_NodeType>` | :ref:`get_node_type<class_XMLParser_method_get_node_type>`\ (\ ) |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`has_attribute<class_XMLParser_method_has_attribute>`\ (\ name\: :ref:`String<class_String>`\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`is_empty<class_XMLParser_method_is_empty>`\ (\ ) |const| |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open<class_XMLParser_method_open>`\ (\ file\: :ref:`String<class_String>`\ ) |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open_buffer<class_XMLParser_method_open_buffer>`\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`read<class_XMLParser_method_read>`\ (\ ) |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`seek<class_XMLParser_method_seek>`\ (\ position\: :ref:`int<class_int>`\ ) |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| |void| | :ref:`skip_section<class_XMLParser_method_skip_section>`\ (\ ) |
|
|
+------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
|
|
.. rst-class:: classref-section-separator
|
|
|
|
----
|
|
|
|
.. rst-class:: classref-descriptions-group
|
|
|
|
Enumerations
|
|
------------
|
|
|
|
.. _enum_XMLParser_NodeType:
|
|
|
|
.. rst-class:: classref-enumeration
|
|
|
|
enum **NodeType**: :ref:`🔗<enum_XMLParser_NodeType>`
|
|
|
|
.. _class_XMLParser_constant_NODE_NONE:
|
|
|
|
.. rst-class:: classref-enumeration-constant
|
|
|
|
:ref:`NodeType<enum_XMLParser_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<enum_XMLParser_NodeType>` **NODE_ELEMENT** = ``1``
|
|
|
|
An element node type, also known as a tag, e.g. ``<title>``.
|
|
|
|
.. _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. ``</title>``.
|
|
|
|
.. _class_XMLParser_constant_NODE_TEXT:
|
|
|
|
.. rst-class:: classref-enumeration-constant
|
|
|
|
:ref:`NodeType<enum_XMLParser_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<enum_XMLParser_NodeType>` **NODE_COMMENT** = ``4``
|
|
|
|
A comment node type, e.g. ``<!--A comment-->``.
|
|
|
|
.. _class_XMLParser_constant_NODE_CDATA:
|
|
|
|
.. rst-class:: classref-enumeration-constant
|
|
|
|
:ref:`NodeType<enum_XMLParser_NodeType>` **NODE_CDATA** = ``5``
|
|
|
|
A node type for CDATA (Character Data) sections, e.g. ``<![CDATA[CDATA section]]>``.
|
|
|
|
.. _class_XMLParser_constant_NODE_UNKNOWN:
|
|
|
|
.. rst-class:: classref-enumeration-constant
|
|
|
|
:ref:`NodeType<enum_XMLParser_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<class_int>` **get_attribute_count**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_attribute_count>`
|
|
|
|
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<class_XMLParser_constant_NODE_ELEMENT>` or :ref:`NODE_ELEMENT_END<class_XMLParser_constant_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<class_String>` **get_attribute_name**\ (\ idx\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_attribute_name>`
|
|
|
|
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<class_String>` **get_attribute_value**\ (\ idx\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_attribute_value>`
|
|
|
|
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<class_int>` **get_current_line**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_current_line>`
|
|
|
|
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<class_String>` **get_named_attribute_value**\ (\ name\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_named_attribute_value>`
|
|
|
|
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<class_String>` **get_named_attribute_value_safe**\ (\ name\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_named_attribute_value_safe>`
|
|
|
|
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<class_String>` **get_node_data**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_node_data>`
|
|
|
|
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<class_String>` **get_node_name**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_node_name>`
|
|
|
|
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<class_XMLParser_constant_NODE_CDATA>` node and the comment string of a :ref:`NODE_COMMENT<class_XMLParser_constant_NODE_COMMENT>` node are also considered names.
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_XMLParser_method_get_node_offset:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`int<class_int>` **get_node_offset**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_node_offset>`
|
|
|
|
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<enum_XMLParser_NodeType>` **get_node_type**\ (\ ) :ref:`🔗<class_XMLParser_method_get_node_type>`
|
|
|
|
Returns the type of the current node. Compare with :ref:`NodeType<enum_XMLParser_NodeType>` constants.
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_XMLParser_method_has_attribute:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **has_attribute**\ (\ name\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_XMLParser_method_has_attribute>`
|
|
|
|
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<class_bool>` **is_empty**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_is_empty>`
|
|
|
|
Returns ``true`` if the currently parsed element is empty, e.g. ``<element />``.
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_XMLParser_method_open:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **open**\ (\ file\: :ref:`String<class_String>`\ ) :ref:`🔗<class_XMLParser_method_open>`
|
|
|
|
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<enum_@GlobalScope_Error>` **open_buffer**\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_XMLParser_method_open_buffer>`
|
|
|
|
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<enum_@GlobalScope_Error>` **read**\ (\ ) :ref:`🔗<class_XMLParser_method_read>`
|
|
|
|
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<enum_@GlobalScope_Error>` **seek**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`🔗<class_XMLParser_method_seek>`
|
|
|
|
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:`🔗<class_XMLParser_method_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.
|
|
|
|
.. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
|
|
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
|
|
.. |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.)`
|