XMLParser
继承: RefCounted
< Object
Provides a low-level interface for creating parsers for XML files.
描述
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 an 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}");
}
}
方法
int | get_attribute_count ( ) const1 |
String | get_attribute_name ( idx: int ) const1 |
String | get_attribute_value ( idx: int ) const1 |
int | get_current_line ( ) const1 |
String | get_named_attribute_value ( name: String ) const1 |
String | get_named_attribute_value_safe ( name: String ) const1 |
String | get_node_data ( ) const1 |
String | get_node_name ( ) const1 |
int | get_node_offset ( ) const1 |
NodeType | get_node_type ( ) |
bool | has_attribute ( name: String ) const1 |
bool | is_empty ( ) const1 |
Error | open ( file: String ) |
Error | open_buffer ( buffer: PackedByteArray ) |
Error | read ( ) |
Error | seek ( position: int ) |
void | skip_section ( ) |
枚举
enum NodeType:
NodeType NODE_NONE = 0
There's no node (no file or buffer opened).
NodeType NODE_ELEMENT = 1
An element node type, also known as a tag, e.g. <title>
.
NodeType NODE_ELEMENT_END = 2
An end of element node type, e.g. </title>
.
NodeType NODE_TEXT = 3
A text node type, i.e. text that is not inside an element. This includes whitespace.
NodeType NODE_COMMENT = 4
A comment node type, e.g. <!--A comment-->
.
NodeType NODE_CDATA = 5
A node type for CDATA (Character Data) sections, e.g. <![CDATA[CDATA section]]>
.
NodeType NODE_UNKNOWN = 6
An unknown node type.
方法说明
int
get_attribute_count ( ) const1
Returns the number of attributes in the currently parsed element.
Note: If this method is used while the currently parsed node is not NODE_ELEMENT
or NODE_ELEMENT_END
, this count will not be updated and will still reflect the last element.
String
get_attribute_name ( idx: int
) const1
Returns the name of an attribute of the currently parsed element, specified by the idx
index.
String
get_attribute_value ( idx: int
) const1
Returns the value of an attribute of the currently parsed element, specified by the idx
index.
int
get_current_line ( ) const1
Returns the current line in the parsed file, counting from 0.
String
get_named_attribute_value ( name: String
) const1
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.
String
get_named_attribute_value_safe ( name: String
) const1
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.
String
get_node_data ( ) const1
Returns the contents of a text node. This method will raise an error if the current parsed node is of any other type.
String
get_node_name ( ) const1
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 ( ) const1
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.
NodeType get_node_type ( )
Returns the type of the current node. Compare with NodeType constants.
bool
has_attribute ( name: String
) const1
Returns true
if the currently parsed element has an attribute with the name
.
Returns true
if the currently parsed element is empty, e.g. <element />
.
Opens an XML file
for parsing. This method returns an error code.
Error open_buffer ( buffer: PackedByteArray
)
Opens an XML raw buffer
for parsing. This method returns an error code.
Error read ( )
Parses the next node in the file. This method returns an error code.
Moves the buffer cursor to a certain offset (since the beginning) and reads the next node there. This method returns an error code.
void
skip_section ( )
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.
本方法通常需要用户覆盖才能生效。
本方法无副作用,不会修改该实例的任何成员变量。
本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。
本方法用于构造某个类型。
调用本方法无需实例,可直接使用类名进行调用。
本方法描述的是使用本类型作为左操作数的有效运算符。
这个值是由下列位标志构成位掩码的整数。
无返回值。