Table of Contents

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 a 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

int

GetAttributeName(int)

Returns the name of an attribute of the currently parsed element, specified by the idx index.

public string GetAttributeName(int idx)

Parameters

idx int

Returns

string

GetAttributeValue(int)

Returns the value of an attribute of the currently parsed element, specified by the idx index.

public string GetAttributeValue(int idx)

Parameters

idx int

Returns

string

GetCurrentLine()

Returns the current line in the parsed file, counting from 0.

public int GetCurrentLine()

Returns

int

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

name string

Returns

string

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

name string

Returns

string

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

string

GetNodeName()

Returns the name of an element node. This method will raise an error if the currently parsed node is not of Element or ElementEnd type.

public string GetNodeName()

Returns

string

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

ulong

GetNodeType()

Returns the type of the current node. Compare with XmlParser.NodeType constants.

public XmlParser.NodeType GetNodeType()

Returns

XmlParser.NodeType

HasAttribute(string)

Returns true if the currently parsed element has an attribute with the name.

public bool HasAttribute(string name)

Parameters

name string

Returns

bool

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

method godot_string_name

Name of the method to check for.

Returns

bool

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

signal godot_string_name

Name of the signal to check for.

Returns

bool

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

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsEmpty()

Returns true if the currently parsed element is empty, e.g. <element />.

public bool IsEmpty()

Returns

bool

Open(string)

Opens an XML file for parsing. This method returns an error code.

public Error Open(string file)

Parameters

file string

Returns

Error

OpenBuffer(byte[])

Opens an XML raw buffer for parsing. This method returns an error code.

public Error OpenBuffer(byte[] buffer)

Parameters

buffer byte[]

Returns

Error

Read()

Parses the next node in the file. This method returns an error code.

public Error Read()

Returns

Error

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

position ulong

Returns

Error

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()