Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
XMLParser¶
Inherits: RefCounted < Object
Provides a low-level interface for creating parsers for XML files.
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 open method or a buffer with the open_buffer method. Then, the 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:
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)
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}");
}
}
Methods¶
get_attribute_count ( ) const |
|
get_attribute_name ( int idx ) const |
|
get_attribute_value ( int idx ) const |
|
get_current_line ( ) const |
|
get_named_attribute_value ( String name ) const |
|
get_named_attribute_value_safe ( String name ) const |