[SOLVED] How to parse XML file on Godot Engine?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kayaocal
:warning: Old Version Published before Godot 3 was released.

I’m trying to learn Godot Engine and I need to parse a simple xml file for my game. I checked XMLParser document, but unfortunately I couldn’t be succeed. My XML file is very simple. I just want to print name variable of lvl1. Here is my xml and code

<?xml version="1.0" encoding="UTF-8"?><game><levels>
<lvl1>
	<name> Level one </string>
</lvl1></levels></game>

var file = XMLParser.new()
file.open("res://Files/game.xml")

Regards.

:bust_in_silhouette: Reply From: volzhs

I guess it’s because of wrong close tag.

<?xml version="1.0" encoding="UTF-8"?><game><levels>
<lvl1>
    <name> Level one </string>  <!-- /string should be /name
</lvl1></levels></game>

Thank you very much. I just miss that one.

How about scripting part? I mean How can I print the value of name?

print(str(file.xxxx))

Which function is this xxxx?

Kayaocal | 2016-05-02 12:34

I overlooked XMLParser source code.
But I don’t know how to do it.

I prefer using json format and using Dictionary.parse_json(json_str)

game.json

{
	"game": {
		"levels": {
			"lvl1": {
				"name": "Level one"
			}
		}
	}
}

script

var file = File.new()
file.open("res://game.json", File.READ)
var json_str = file.get_as_text()
var game_data = {}
game_data.parse_json(json_str)
print(game_data.game.levels.lvl1.name)

volzhs | 2016-05-02 13:11