| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="XMLParser" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Provides a low-level interface for creating parsers for XML files. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | Provides a low-level interface for creating parsers for [url=https://en.wikipedia.org/wiki/XML]XML[/url] files. This class can serve as base to make custom XML parsers. |
| 8 | To parse XML, you must open a file with the [method open] method or a buffer with the [method open_buffer] method. Then, the [method read] method must be called to parse the next nodes. Most of the methods take into consideration the currently parsed node. |
| 9 | 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: |
| 10 | [codeblocks] |
| 11 | [gdscript] |
| 12 | var parser = XMLParser.new() |
| 13 | parser.open("path/to/file.svg") |
| 14 | while parser.read() != ERR_FILE_EOF: |
| 15 | if parser.get_node_type() == XMLParser.NODE_ELEMENT: |
| 16 | var node_name = parser.get_node_name() |
| 17 | var attributes_dict = {} |
| 18 | for idx in range(parser.get_attribute_count()): |
| 19 | attributes_dict[parser.get_attribute_name(idx)] = parser.get_attribute_value(idx) |
| 20 | print("The ", node_name, " element has the following attributes: ", attributes_dict) |
| 21 | [/gdscript] |
| 22 | [csharp] |
| 23 | var parser = new XmlParser(); |
| 24 | parser.Open("path/to/file.svg"); |
| 25 | while (parser.Read() != Error.FileEof) |
| 26 | { |
| 27 | if (parser.GetNodeType() == XmlParser.NodeType.Element) |
| 28 | { |
| 29 | var nodeName = parser.GetNodeName(); |
| 30 | var attributesDict = new Godot.Collections.Dictionary(); |
| 31 | for (int idx = 0; idx < parser.GetAttributeCount(); idx++) |
| 32 | { |
| 33 | attributesDict[parser.GetAttributeName(idx)] = parser.GetAttributeValue(idx); |
| 34 | } |
| 35 | GD.Print($"The {nodeName} element has the following attributes: {attributesDict}"); |
| 36 | } |
| 37 | } |
| 38 | [/csharp] |
| 39 | [/codeblocks] |
| 40 | </description> |
| 41 | <tutorials> |
| 42 | </tutorials> |
| 43 | <methods> |
| 44 | <method name="get_attribute_count" qualifiers="const"> |
| 45 | <return type="int" /> |
| 46 | <description> |
| 47 | Returns the number of attributes in the currently parsed element. |
| 48 | [b]Note:[/b] If this method is used while the currently parsed node is not [constant NODE_ELEMENT] or [constant NODE_ELEMENT_END], this count will not be updated and will still reflect the last element. |
| 49 | </description> |
| 50 | </method> |
| 51 | <method name="get_attribute_name" qualifiers="const"> |
| 52 | <return type="String" /> |
| 53 | <param index="0" name="idx" type="int" /> |
| 54 | <description> |
| 55 | Returns the name of an attribute of the currently parsed element, specified by the [param idx] index. |
| 56 | </description> |
| 57 | </method> |
| 58 | <method name="get_attribute_value" qualifiers="const"> |
| 59 | <return type="String" /> |
| 60 | <param index="0" name="idx" type="int" /> |
| 61 | <description> |
| 62 | Returns the value of an attribute of the currently parsed element, specified by the [param idx] index. |
| 63 | </description> |
| 64 | </method> |
| 65 | <method name="get_current_line" qualifiers="const"> |
| 66 | <return type="int" /> |
| 67 | <description> |
| 68 | Returns the current line in the parsed file, counting from 0. |
| 69 | </description> |
| 70 | </method> |
| 71 | <method name="get_named_attribute_value" qualifiers="const"> |
| 72 | <return type="String" /> |
| 73 | <param index="0" name="name" type="String" /> |
| 74 | <description> |
| 75 | Returns the value of an attribute of the currently parsed element, specified by its [param name]. This method will raise an error if the element has no such attribute. |
| 76 | </description> |
| 77 | </method> |
| 78 | <method name="get_named_attribute_value_safe" qualifiers="const"> |
| 79 | <return type="String" /> |
| 80 | <param index="0" name="name" type="String" /> |
| 81 | <description> |
| 82 | Returns the value of an attribute of the currently parsed element, specified by its [param name]. This method will return an empty string if the element has no such attribute. |
| 83 | </description> |
| 84 | </method> |
| 85 | <method name="get_node_data" qualifiers="const"> |
| 86 | <return type="String" /> |
| 87 | <description> |
| 88 | Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type. |
| 89 | </description> |
| 90 | </method> |
| 91 | <method name="get_node_name" qualifiers="const"> |
| 92 | <return type="String" /> |
| 93 | <description> |
| 94 | Returns the name of a node. This method will raise an error if the currently parsed node is a text node. |
| 95 | [b]Note:[/b] The content of a [constant NODE_CDATA] node and the comment string of a [constant NODE_COMMENT] node are also considered names. |
| 96 | </description> |
| 97 | </method> |
| 98 | <method name="get_node_offset" qualifiers="const"> |
| 99 | <return type="int" /> |
| 100 | <description> |
| 101 | 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. |
| 102 | </description> |
| 103 | </method> |
| 104 | <method name="get_node_type"> |
| 105 | <return type="int" enum="XMLParser.NodeType" /> |
| 106 | <description> |
| 107 | Returns the type of the current node. Compare with [enum NodeType] constants. |
| 108 | </description> |
| 109 | </method> |
| 110 | <method name="has_attribute" qualifiers="const"> |
| 111 | <return type="bool" /> |
| 112 | <param index="0" name="name" type="String" /> |
| 113 | <description> |
| 114 | Returns [code]true[/code] if the currently parsed element has an attribute with the [param name]. |
| 115 | </description> |
| 116 | </method> |
| 117 | <method name="is_empty" qualifiers="const"> |
| 118 | <return type="bool" /> |
| 119 | <description> |
| 120 | Returns [code]true[/code] if the currently parsed element is empty, e.g. [code]<element />[/code]. |
| 121 | </description> |
| 122 | </method> |
| 123 | <method name="open"> |
| 124 | <return type="int" enum="Error" /> |
| 125 | <param index="0" name="file" type="String" /> |
| 126 | <description> |
| 127 | Opens an XML [param file] for parsing. This method returns an error code. |
| 128 | </description> |
| 129 | </method> |
| 130 | <method name="open_buffer"> |
| 131 | <return type="int" enum="Error" /> |
| 132 | <param index="0" name="buffer" type="PackedByteArray" /> |
| 133 | <description> |
| 134 | Opens an XML raw [param buffer] for parsing. This method returns an error code. |
| 135 | </description> |
| 136 | </method> |
| 137 | <method name="read"> |
| 138 | <return type="int" enum="Error" /> |
| 139 | <description> |
| 140 | Parses the next node in the file. This method returns an error code. |
| 141 | </description> |
| 142 | </method> |
| 143 | <method name="seek"> |
| 144 | <return type="int" enum="Error" /> |
| 145 | <param index="0" name="position" type="int" /> |
| 146 | <description> |
| 147 | Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code. |
| 148 | </description> |
| 149 | </method> |
| 150 | <method name="skip_section"> |
| 151 | <return type="void" /> |
| 152 | <description> |
| 153 | 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. |
| 154 | </description> |
| 155 | </method> |
| 156 | </methods> |
| 157 | <constants> |
| 158 | <constant name="NODE_NONE" value="0" enum="NodeType"> |
| 159 | There's no node (no file or buffer opened). |
| 160 | </constant> |
| 161 | <constant name="NODE_ELEMENT" value="1" enum="NodeType"> |
| 162 | An element node type, also known as a tag, e.g. [code]<title>[/code]. |
| 163 | </constant> |
| 164 | <constant name="NODE_ELEMENT_END" value="2" enum="NodeType"> |
| 165 | An end of element node type, e.g. [code]</title>[/code]. |
| 166 | </constant> |
| 167 | <constant name="NODE_TEXT" value="3" enum="NodeType"> |
| 168 | A text node type, i.e. text that is not inside an element. This includes whitespace. |
| 169 | </constant> |
| 170 | <constant name="NODE_COMMENT" value="4" enum="NodeType"> |
| 171 | A comment node type, e.g. [code]<!--A comment-->[/code]. |
| 172 | </constant> |
| 173 | <constant name="NODE_CDATA" value="5" enum="NodeType"> |
| 174 | A node type for CDATA (Character Data) sections, e.g. [code]<![CDATA[CDATA section]]>[/code]. |
| 175 | </constant> |
| 176 | <constant name="NODE_UNKNOWN" value="6" enum="NodeType"> |
| 177 | An unknown node type. |
| 178 | </constant> |
| 179 | </constants> |
| 180 | </class> |