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 |
|
get_node_data ( ) const |
|
get_node_name ( ) const |
|
get_node_offset ( ) const |
|
get_node_type ( ) |
|
has_attribute ( String name ) const |
|
is_empty ( ) const |
|
open_buffer ( PackedByteArray buffer ) |
|
read ( ) |
|
void |
skip_section ( ) |