2017-09-12 22:42:36 +02:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 10:08:05 +02:00
<class name= "XMLParser" inherits= "RefCounted" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2017-09-12 22:42:36 +02:00
<brief_description >
2023-04-28 01:35:33 +02:00
Provides a low-level interface for creating parsers for XML files.
2017-09-12 22:42:36 +02:00
</brief_description>
<description >
2023-04-28 01:35:33 +02:00
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.
2023-08-25 16:55:57 +02:00
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.
Here is an example of using [XMLParser] to parse a SVG file (which is based on XML), printing each element and its attributes as a dictionary:
[codeblocks]
[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)
[/gdscript]
[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}");
}
}
[/csharp]
[/codeblocks]
2017-09-12 22:42:36 +02:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "get_attribute_count" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "int" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the number of attributes in the currently parsed element.
[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.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_attribute_name" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "idx" type= "int" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the name of an attribute of the currently parsed element, specified by the [param idx] index.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_attribute_value" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "idx" type= "int" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the value of an attribute of the currently parsed element, specified by the [param idx] index.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_current_line" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "int" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the current line in the parsed file, counting from 0.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_named_attribute_value" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "name" type= "String" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
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.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_named_attribute_value_safe" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "name" type= "String" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
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.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_node_data" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_node_name" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the name of an element node. This method will raise an error if the currently parsed node is not of [constant NODE_ELEMENT] or [constant NODE_ELEMENT_END] type.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_node_offset" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "int" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
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.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "get_node_type" >
2021-07-30 15:28:05 +02:00
<return type= "int" enum= "XMLParser.NodeType" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns the type of the current node. Compare with [enum NodeType] constants.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "has_attribute" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "bool" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "name" type= "String" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns [code]true[/code] if the currently parsed element has an attribute with the [param name].
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "is_empty" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "bool" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Returns [code]true[/code] if the currently parsed element is empty, e.g. [code]< element /> [/code].
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "open" >
2021-07-30 15:28:05 +02:00
<return type= "int" enum= "Error" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "file" type= "String" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Opens an XML [param file] for parsing. This method returns an error code.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "open_buffer" >
2021-07-30 15:28:05 +02:00
<return type= "int" enum= "Error" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "buffer" type= "PackedByteArray" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Opens an XML raw [param buffer] for parsing. This method returns an error code.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "read" >
2021-07-30 15:28:05 +02:00
<return type= "int" enum= "Error" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Parses the next node in the file. This method returns an error code.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "seek" >
2021-07-30 15:28:05 +02:00
<return type= "int" enum= "Error" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "position" type= "int" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code.
2017-09-12 22:42:36 +02:00
</description>
</method>
<method name= "skip_section" >
2021-07-30 15:28:05 +02:00
<return type= "void" />
2017-09-12 22:42:36 +02:00
<description >
2023-08-25 16:55:57 +02:00
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.
2017-09-12 22:42:36 +02:00
</description>
</method>
</methods>
<constants >
2017-11-24 23:16:30 +01:00
<constant name= "NODE_NONE" value= "0" enum= "NodeType" >
2020-01-23 11:14:14 +01:00
There's no node (no file or buffer opened).
2017-09-12 22:42:36 +02:00
</constant>
2017-11-24 23:16:30 +01:00
<constant name= "NODE_ELEMENT" value= "1" enum= "NodeType" >
2023-08-25 16:55:57 +02:00
An element node type, also known as a tag, e.g. [code]< title> [/code].
2017-09-12 22:42:36 +02:00
</constant>
2017-11-24 23:16:30 +01:00
<constant name= "NODE_ELEMENT_END" value= "2" enum= "NodeType" >
2023-08-25 16:55:57 +02:00
An end of element node type, e.g. [code]< /title> [/code].
2017-09-12 22:42:36 +02:00
</constant>
2017-11-24 23:16:30 +01:00
<constant name= "NODE_TEXT" value= "3" enum= "NodeType" >
2023-08-25 16:55:57 +02:00
A text node type, i.e. text that is not inside an element. This includes whitespace.
2017-09-12 22:42:36 +02:00
</constant>
2017-11-24 23:16:30 +01:00
<constant name= "NODE_COMMENT" value= "4" enum= "NodeType" >
2023-08-25 16:55:57 +02:00
A comment node type, e.g. [code]< !--A comment--> [/code].
2017-09-12 22:42:36 +02:00
</constant>
2017-11-24 23:16:30 +01:00
<constant name= "NODE_CDATA" value= "5" enum= "NodeType" >
2023-08-25 16:55:57 +02:00
A node type for CDATA (Character Data) sections, e.g. [code]< ![CDATA[CDATA section]]> [/code].
2017-09-12 22:42:36 +02:00
</constant>
2017-11-24 23:16:30 +01:00
<constant name= "NODE_UNKNOWN" value= "6" enum= "NodeType" >
2023-08-25 16:55:57 +02:00
An unknown node type.
2017-09-12 22:42:36 +02:00
</constant>
</constants>
</class>