Class XmlParser
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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(string) method or a buffer with the OpenBuffer(byte[]) 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 an SVG file (which is based on XML), printing each element and its attributes as a dictionary:
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}");
}
}
[GodotClassName("XMLParser")]
public class XmlParser : RefCounted, IDisposable
- Inheritance
-
XmlParser
- Implements
- Inherited Members
Constructors
XmlParser()
public XmlParser()
Methods
GetAttributeCount()
Returns the number of attributes in the currently parsed element.
Note: If this method is used while the currently parsed node is not Element or ElementEnd, this count will not be updated and will still reflect the last element.
public int GetAttributeCount()
Returns
GetAttributeName(int)
Returns the name of an attribute of the currently parsed element, specified by the idx index.
public string GetAttributeName(int idx)
Parameters
idxint
Returns
GetAttributeValue(int)
Returns the value of an attribute of the currently parsed element, specified by the idx index.
public string GetAttributeValue(int idx)
Parameters
idxint
Returns
GetCurrentLine()
Returns the current line in the parsed file, counting from 0.
public int GetCurrentLine()
Returns
GetNamedAttributeValue(string)
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.
public string GetNamedAttributeValue(string name)
Parameters
namestring
Returns
GetNamedAttributeValueSafe(string)
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.
public string GetNamedAttributeValueSafe(string name)
Parameters
namestring
Returns
GetNodeData()
Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type.
public string GetNodeData()
Returns
GetNodeName()
Returns the name of a node. This method will raise an error if the currently parsed node is a text node.
Note: The content of a Cdata node and the comment string of a Comment node are also considered names.
public string GetNodeName()
Returns
GetNodeOffset()
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.
public ulong GetNodeOffset()
Returns
GetNodeType()
Returns the type of the current node. Compare with XmlParser.NodeType constants.
public XmlParser.NodeType GetNodeType()
Returns
HasAttribute(string)
Returns true if the currently parsed element has an attribute with the name.
public bool HasAttribute(string name)
Parameters
namestring
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
methodgodot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signalgodot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
methodgodot_string_nameName of the method to invoke.
argsNativeVariantPtrArgsArguments to use with the invoked method.
retgodot_variantValue returned by the invoked method.
Returns
IsEmpty()
Returns true if the currently parsed element is empty, e.g. <element />.
public bool IsEmpty()
Returns
Open(string)
Opens an XML file for parsing. This method returns an error code.
public Error Open(string file)
Parameters
filestring
Returns
OpenBuffer(byte[])
Opens an XML raw buffer for parsing. This method returns an error code.
public Error OpenBuffer(byte[] buffer)
Parameters
bufferbyte[]
Returns
Read()
Parses the next node in the file. This method returns an error code.
public Error Read()
Returns
Seek(ulong)
Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code.
public Error Seek(ulong position)
Parameters
positionulong
Returns
SkipSection()
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.
public void SkipSection()