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.

XMLParser

繼承: RefCounted < Object

為建立 XML 檔解析器提供低階介面。

說明

為建立 XML 檔解析器提供低階介面。製作自訂 XML 解析器時,可以將這個類作為基礎。

要解析 XML,你必須使用 open() 方法打開檔,或者使用 open_buffer() 方法打開緩衝區。然後必須使用 read() 方法解析後續節點。大多數方法使用的是目前解析節點。

以下是使用 XMLParser 解析 SVG 檔(基於 XML)的例子,會輸出所有的元素,以字典的形式輸出對應的屬性:

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("元素 ", node_name, " 包含的屬性有:", attributes_dict)

方法

int

get_attribute_count() const

String

get_attribute_name(idx: int) const

String

get_attribute_value(idx: int) const

int

get_current_line() const

String

get_named_attribute_value(name: String) const

String

get_named_attribute_value_safe(name: String) const

String

get_node_data() const

String

get_node_name() const

int

get_node_offset() const

NodeType

get_node_type()

bool

has_attribute(name: String) const

bool

is_empty() const

Error

open(file: String)

Error

open_buffer(buffer: PackedByteArray)

Error

read()

Error

seek(position: int)

void

skip_section()


列舉

enum NodeType: 🔗

NodeType NODE_NONE = 0

沒有節點,未打開檔或緩衝區。

NodeType NODE_ELEMENT = 1

元素節點型別,也稱作標籤,例如 <title>

NodeType NODE_ELEMENT_END = 2

元素結束節點型別,例如 </title>

NodeType NODE_TEXT = 3

文字節點型別,即不在元素中的文字。包含空白字元。

NodeType NODE_COMMENT = 4

注釋節點型別,例如 <!--A comment-->

NodeType NODE_CDATA = 5

CDATA(字元資料)部分對應的節點型別,例如 <![CDATA[CDATA section]]>

NodeType NODE_UNKNOWN = 6

未知節點型別。


方法說明

int get_attribute_count() const 🔗

返回目前解析元素中屬性的數量。

注意:如果在目前解析的節點不是 NODE_ELEMENTNODE_ELEMENT_END 時呼叫這個方法,則該計數不會更新,仍然反映的是之前的元素。


String get_attribute_name(idx: int) const 🔗

返回目前解析元素中某個屬性的名稱,屬性由 idx 索引指定。


String get_attribute_value(idx: int) const 🔗

返回目前解析元素中某個屬性的取值,屬性由 idx 索引指定。


int get_current_line() const 🔗

返回解析檔中的目前行,從 0 開始計數。


String get_named_attribute_value(name: String) const 🔗

返回目前解析元素中某個屬性的取值,屬性由名稱 name 指定。如果該元素沒有符合要求的屬性,則會引發錯誤。


String get_named_attribute_value_safe(name: String) const 🔗

返回目前解析元素中某個屬性的取值,屬性由名稱 name 指定。如果該元素沒有符合要求的屬性,則會返回空字串。


String get_node_data() const 🔗

返回文字節點的內容。如果目前解析節點是其他型別,則會引發錯誤。


String get_node_name() const 🔗

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 NODE_CDATA node and the comment string of a NODE_COMMENT node are also considered names.


int get_node_offset() const 🔗

返回目前解析節點相對於檔或緩衝區開始處的位元組偏移量。通常等價於讀取位置之前的字元數。


NodeType get_node_type() 🔗

返回目前節點的型別。請使用 NodeType 常數進行比較。


bool has_attribute(name: String) const 🔗

如果目前解析元素存在名為 name 的屬性,則返回 true


bool is_empty() const 🔗

如果目前解析元素為空則返回 true,例如 <element />


Error open(file: String) 🔗

打開 XML 檔 file 進行解析。這個方法返回的是錯誤碼。


Error open_buffer(buffer: PackedByteArray) 🔗

打開 XML 原始緩衝區 buffer 進行解析。這個方法返回的是錯誤碼。


Error read() 🔗

解析檔的下一個節點。這個方法返回的是錯誤碼。


Error seek(position: int) 🔗

將緩衝區游標移動到某一偏移量(相對於開始位置)並在那裡讀取下一個節點。這個方法返回的是錯誤碼。


void skip_section() 🔗

跳過目前部分。如果目前解析的節點包含其他內部節點,則會忽略這些節點,游標將跳轉到目前元素的結尾處。