class_xmlparser.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/XMLParser.xml.
  6. .. _class_XMLParser:
  7. XMLParser
  8. =========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Provides a low-level interface for creating parsers for XML files.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. 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.
  16. 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:
  17. .. tabs::
  18. .. code-tab:: gdscript
  19. var parser = XMLParser.new()
  20. parser.open("path/to/file.svg")
  21. while parser.read() != ERR_FILE_EOF:
  22. if parser.get_node_type() == XMLParser.NODE_ELEMENT:
  23. var node_name = parser.get_node_name()
  24. var attributes_dict = {}
  25. for idx in range(parser.get_attribute_count()):
  26. attributes_dict[parser.get_attribute_name(idx)] = parser.get_attribute_value(idx)
  27. print("The ", node_name, " element has the following attributes: ", attributes_dict)
  28. .. code-tab:: csharp
  29. var parser = new XmlParser();
  30. parser.Open("path/to/file.svg");
  31. while (parser.Read() != Error.FileEof)
  32. {
  33. if (parser.GetNodeType() == XmlParser.NodeType.Element)
  34. {
  35. var nodeName = parser.GetNodeName();
  36. var attributesDict = new Godot.Collections.Dictionary();
  37. for (int idx = 0; idx < parser.GetAttributeCount(); idx++)
  38. {
  39. attributesDict[parser.GetAttributeName(idx)] = parser.GetAttributeValue(idx);
  40. }
  41. GD.Print($"The {nodeName} element has the following attributes: {attributesDict}");
  42. }
  43. }
  44. .. rst-class:: classref-reftable-group
  45. Methods
  46. -------
  47. .. table::
  48. :widths: auto
  49. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`int<class_int>` | :ref:`get_attribute_count<class_XMLParser_method_get_attribute_count>`\ (\ ) |const| |
  51. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`String<class_String>` | :ref:`get_attribute_name<class_XMLParser_method_get_attribute_name>`\ (\ idx\: :ref:`int<class_int>`\ ) |const| |
  53. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`String<class_String>` | :ref:`get_attribute_value<class_XMLParser_method_get_attribute_value>`\ (\ idx\: :ref:`int<class_int>`\ ) |const| |
  55. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`int<class_int>` | :ref:`get_current_line<class_XMLParser_method_get_current_line>`\ (\ ) |const| |
  57. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`String<class_String>` | :ref:`get_named_attribute_value<class_XMLParser_method_get_named_attribute_value>`\ (\ name\: :ref:`String<class_String>`\ ) |const| |
  59. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`String<class_String>` | :ref:`get_named_attribute_value_safe<class_XMLParser_method_get_named_attribute_value_safe>`\ (\ name\: :ref:`String<class_String>`\ ) |const| |
  61. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`String<class_String>` | :ref:`get_node_data<class_XMLParser_method_get_node_data>`\ (\ ) |const| |
  63. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`String<class_String>` | :ref:`get_node_name<class_XMLParser_method_get_node_name>`\ (\ ) |const| |
  65. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`int<class_int>` | :ref:`get_node_offset<class_XMLParser_method_get_node_offset>`\ (\ ) |const| |
  67. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`NodeType<enum_XMLParser_NodeType>` | :ref:`get_node_type<class_XMLParser_method_get_node_type>`\ (\ ) |
  69. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`bool<class_bool>` | :ref:`has_attribute<class_XMLParser_method_has_attribute>`\ (\ name\: :ref:`String<class_String>`\ ) |const| |
  71. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`bool<class_bool>` | :ref:`is_empty<class_XMLParser_method_is_empty>`\ (\ ) |const| |
  73. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open<class_XMLParser_method_open>`\ (\ file\: :ref:`String<class_String>`\ ) |
  75. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open_buffer<class_XMLParser_method_open_buffer>`\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
  77. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`read<class_XMLParser_method_read>`\ (\ ) |
  79. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`seek<class_XMLParser_method_seek>`\ (\ position\: :ref:`int<class_int>`\ ) |
  81. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | |void| | :ref:`skip_section<class_XMLParser_method_skip_section>`\ (\ ) |
  83. +------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
  84. .. rst-class:: classref-section-separator
  85. ----
  86. .. rst-class:: classref-descriptions-group
  87. Enumerations
  88. ------------
  89. .. _enum_XMLParser_NodeType:
  90. .. rst-class:: classref-enumeration
  91. enum **NodeType**: :ref:`🔗<enum_XMLParser_NodeType>`
  92. .. _class_XMLParser_constant_NODE_NONE:
  93. .. rst-class:: classref-enumeration-constant
  94. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_NONE** = ``0``
  95. There's no node (no file or buffer opened).
  96. .. _class_XMLParser_constant_NODE_ELEMENT:
  97. .. rst-class:: classref-enumeration-constant
  98. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_ELEMENT** = ``1``
  99. An element node type, also known as a tag, e.g. ``<title>``.
  100. .. _class_XMLParser_constant_NODE_ELEMENT_END:
  101. .. rst-class:: classref-enumeration-constant
  102. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_ELEMENT_END** = ``2``
  103. An end of element node type, e.g. ``</title>``.
  104. .. _class_XMLParser_constant_NODE_TEXT:
  105. .. rst-class:: classref-enumeration-constant
  106. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_TEXT** = ``3``
  107. A text node type, i.e. text that is not inside an element. This includes whitespace.
  108. .. _class_XMLParser_constant_NODE_COMMENT:
  109. .. rst-class:: classref-enumeration-constant
  110. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_COMMENT** = ``4``
  111. A comment node type, e.g. ``<!--A comment-->``.
  112. .. _class_XMLParser_constant_NODE_CDATA:
  113. .. rst-class:: classref-enumeration-constant
  114. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_CDATA** = ``5``
  115. A node type for CDATA (Character Data) sections, e.g. ``<![CDATA[CDATA section]]>``.
  116. .. _class_XMLParser_constant_NODE_UNKNOWN:
  117. .. rst-class:: classref-enumeration-constant
  118. :ref:`NodeType<enum_XMLParser_NodeType>` **NODE_UNKNOWN** = ``6``
  119. An unknown node type.
  120. .. rst-class:: classref-section-separator
  121. ----
  122. .. rst-class:: classref-descriptions-group
  123. Method Descriptions
  124. -------------------
  125. .. _class_XMLParser_method_get_attribute_count:
  126. .. rst-class:: classref-method
  127. :ref:`int<class_int>` **get_attribute_count**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_attribute_count>`
  128. Returns the number of attributes in the currently parsed element.
  129. \ **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.
  130. .. rst-class:: classref-item-separator
  131. ----
  132. .. _class_XMLParser_method_get_attribute_name:
  133. .. rst-class:: classref-method
  134. :ref:`String<class_String>` **get_attribute_name**\ (\ idx\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_attribute_name>`
  135. Returns the name of an attribute of the currently parsed element, specified by the ``idx`` index.
  136. .. rst-class:: classref-item-separator
  137. ----
  138. .. _class_XMLParser_method_get_attribute_value:
  139. .. rst-class:: classref-method
  140. :ref:`String<class_String>` **get_attribute_value**\ (\ idx\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_attribute_value>`
  141. Returns the value of an attribute of the currently parsed element, specified by the ``idx`` index.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. _class_XMLParser_method_get_current_line:
  145. .. rst-class:: classref-method
  146. :ref:`int<class_int>` **get_current_line**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_current_line>`
  147. Returns the current line in the parsed file, counting from 0.
  148. .. rst-class:: classref-item-separator
  149. ----
  150. .. _class_XMLParser_method_get_named_attribute_value:
  151. .. rst-class:: classref-method
  152. :ref:`String<class_String>` **get_named_attribute_value**\ (\ name\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_named_attribute_value>`
  153. 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.
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_XMLParser_method_get_named_attribute_value_safe:
  157. .. rst-class:: classref-method
  158. :ref:`String<class_String>` **get_named_attribute_value_safe**\ (\ name\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_XMLParser_method_get_named_attribute_value_safe>`
  159. 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.
  160. .. rst-class:: classref-item-separator
  161. ----
  162. .. _class_XMLParser_method_get_node_data:
  163. .. rst-class:: classref-method
  164. :ref:`String<class_String>` **get_node_data**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_node_data>`
  165. Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type.
  166. .. rst-class:: classref-item-separator
  167. ----
  168. .. _class_XMLParser_method_get_node_name:
  169. .. rst-class:: classref-method
  170. :ref:`String<class_String>` **get_node_name**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_node_name>`
  171. Returns the name of a node. This method will raise an error if the currently parsed node is a text node.
  172. \ **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.
  173. .. rst-class:: classref-item-separator
  174. ----
  175. .. _class_XMLParser_method_get_node_offset:
  176. .. rst-class:: classref-method
  177. :ref:`int<class_int>` **get_node_offset**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_get_node_offset>`
  178. 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.
  179. .. rst-class:: classref-item-separator
  180. ----
  181. .. _class_XMLParser_method_get_node_type:
  182. .. rst-class:: classref-method
  183. :ref:`NodeType<enum_XMLParser_NodeType>` **get_node_type**\ (\ ) :ref:`🔗<class_XMLParser_method_get_node_type>`
  184. Returns the type of the current node. Compare with :ref:`NodeType<enum_XMLParser_NodeType>` constants.
  185. .. rst-class:: classref-item-separator
  186. ----
  187. .. _class_XMLParser_method_has_attribute:
  188. .. rst-class:: classref-method
  189. :ref:`bool<class_bool>` **has_attribute**\ (\ name\: :ref:`String<class_String>`\ ) |const| :ref:`🔗<class_XMLParser_method_has_attribute>`
  190. Returns ``true`` if the currently parsed element has an attribute with the ``name``.
  191. .. rst-class:: classref-item-separator
  192. ----
  193. .. _class_XMLParser_method_is_empty:
  194. .. rst-class:: classref-method
  195. :ref:`bool<class_bool>` **is_empty**\ (\ ) |const| :ref:`🔗<class_XMLParser_method_is_empty>`
  196. Returns ``true`` if the currently parsed element is empty, e.g. ``<element />``.
  197. .. rst-class:: classref-item-separator
  198. ----
  199. .. _class_XMLParser_method_open:
  200. .. rst-class:: classref-method
  201. :ref:`Error<enum_@GlobalScope_Error>` **open**\ (\ file\: :ref:`String<class_String>`\ ) :ref:`🔗<class_XMLParser_method_open>`
  202. Opens an XML ``file`` for parsing. This method returns an error code.
  203. .. rst-class:: classref-item-separator
  204. ----
  205. .. _class_XMLParser_method_open_buffer:
  206. .. rst-class:: classref-method
  207. :ref:`Error<enum_@GlobalScope_Error>` **open_buffer**\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_XMLParser_method_open_buffer>`
  208. Opens an XML raw ``buffer`` for parsing. This method returns an error code.
  209. .. rst-class:: classref-item-separator
  210. ----
  211. .. _class_XMLParser_method_read:
  212. .. rst-class:: classref-method
  213. :ref:`Error<enum_@GlobalScope_Error>` **read**\ (\ ) :ref:`🔗<class_XMLParser_method_read>`
  214. Parses the next node in the file. This method returns an error code.
  215. .. rst-class:: classref-item-separator
  216. ----
  217. .. _class_XMLParser_method_seek:
  218. .. rst-class:: classref-method
  219. :ref:`Error<enum_@GlobalScope_Error>` **seek**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`🔗<class_XMLParser_method_seek>`
  220. Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code.
  221. .. rst-class:: classref-item-separator
  222. ----
  223. .. _class_XMLParser_method_skip_section:
  224. .. rst-class:: classref-method
  225. |void| **skip_section**\ (\ ) :ref:`🔗<class_XMLParser_method_skip_section>`
  226. 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.
  227. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  228. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  229. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  230. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  231. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  232. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  233. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  234. .. |void| replace:: :abbr:`void (No return value.)`