0 votes

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.

in Engine by (23 points)
edited by

1 Answer

+1 vote
Best answer

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>
by (9,786 points)
selected by

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?

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)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.